I thought I had already a decent grasp of Git, but just now I've been surprised. I thought that branches would isolate the changes from other branches, so I started a big experimental refactoring in a new branch. This re开发者_运维问答factoring, implied moving many files. When I switched to master I could still see the changes though!
I went to my sandbox repository to replicate the problem:
$ git branch crazy-refactoring
$ git checkout crazy-refactoring
$ rm README
$ git checkout master
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: README
#
no changes added to commit (use "git add" and/or "git commit -a")
$ ls README
ls: cannot access README: No such file or directory
Why? The whole point of me creating a branch, was that I could try something, and throw it away if unsuccessful. Toughts?
You did not commit the delete. Only committed changes are "isolated".
Regarding your comment:
Maybe I'm missing something, but it seems to me a weird and undesirable behavior.
Think about it:
You deleted README
in crazy-refactoring
but did not commit the change. Git knows that you changed the file and that you did not commit it. In order to preserve your changes, it does not override them if you checkout another branch.
Otherwise there would be no way for Git to know what the changes have been when you switch back to branch you made the changes in. They would be lost, and that is far more worse than seeing the changes in the other branch.
You current working directory is always current branch + uncommitted changes (unless you reset them).
Of course, Git could warn you when checking out the other branch, that you have uncommitted changes, but sometimes it is desired to keep the changes and switch to another branch.
精彩评论