问题
I am working locally. I checked out a branch "b1" and did some work on it. I want to keep the branch "b1" the way it is but go back to the master and for it to be like it was before I checked out "b1". But when I check out master with "git checkout master" all the changes I made in the branch are already in the master without any commits or merges. What am I doing wrong?
回答1:
Did you commit your changes to b1
before checking out master
again?
If you do not commit or stash your changes before switching branches, your changes will remain in your local files, therefore will appear whichever branch you have checked out.
回答2:
If the changes you've made to the branch b1
would be overwritten by checking out master
git won't let you do the checkout without you either stashing the changes or committing the changes to the index. You get the following kind of error
error: Your local changes to the following files would be overwritten by checkout:
/src/path/to/conflicting/file.java
Please, commit your changes or stash them before you can switch branches.
If the checkout goes through without a hitch (without you having committed anything) then there are no conflicts between the two branches and git will just keep the working tree as it is.
What you need to do is checkout b1
do a commit there of all the changes you made and want to localise to b1
and then checkout master
. The changes should now not be in the master
branch.
来源:https://stackoverflow.com/questions/12144844/confused-about-git-branches-and-master-interaction