Secure branch switching in git

I am on the main branch and want to work on a new function, so I:

git checkout -b new_feature 

Now I am coding, git state execution shows modified files.

If I try to switch to the main branch, it says that I cannot update the b / c files.

ok, so I will add them:

 git add -A 

Now, if I try to go to the master branch, it seems like I need to merge ???

 git checkout master 

What I want to know how to do this:

  • while in another branch, I want to stop what I'm doing and go to the main branch, and not merge anything else, I just want to stop working on a new branch of functions and return to the wizard.
+7
source share
4 answers

You have two options (at least). Performing "work in progress" or cache.

"Work in progress" does just that: it is a commit in a branch that represents work in progress:

 $ git commit -am'WIP (description here)' $ git checkout master 

Later, when you return to your branch, you can continue to work and do:

 $ git checkout mybranch # do work $ git commit -am'Finish WIP (description here)' 

When you are done, you can merge your WIP if you want, commit to commits without any evidence that you did partial work:

 $ git rebase -i HEAD~2 # See instructions for rebase -i elsewhere 

Or you can use the cache:

 $ git add . $ git stash save 'WIP (descriptino here)' $ git checkout master 

Later, when you return to your branch:

 $ git checkout my_branch $ git stash pop 

You will be right where you left off.

+8
source

git stash may be what you are looking for.

+2
source

It seems that you forgot commit to make changes to add to the index.

 git add <something> git commit git checkout master 
+2
source

By doing git add -A, you simply add your changes to the index, that is, you tell git that these changes will be the ones you make after executing the git commit. You haven't actually done anything, so when you try to check something else, git warns you because you will lose your index. git commit -m "Your commit message" will add indexed changes to the tree on top of new_feature and remove them from the index, so you can switch the branch right away.

Git stash is another option, but depending on how likely your changes to new_features are to create conflicts, you might prefer to commit (you can then reset --mixed HEAD ~ 1 to go up with one commit without changing the working copy).

0
source

All Articles