I have a branch called experiment.
git checkout master
echo 'some changes' > a.txt
# now master branch has local changes which are not committed
git checkout experiment
Sometimes I have noticed that I am not allowed to switch to another branch if I have local changes. And sometimes I am allowed to switch to another branch if I have local changes.
What am I missing?
An excerpt from the git checkout manpage:
So, if the modifications don’t have anything to do with the differences between the branches (as in your example) it’ll let you switch. If the diff between the branches modifies the same file you have, it’ll refuse – but you can specify the
-moption to get it to do a three-way merge between current branch, work tree, and new branch (that’s where that snippet is from).To make that description a bit more complete: suppose the diff between master and experimental is only in files A, B, and C. If in your work tree you modify A, you will not be able to check out the other branch. If you modify D, though, you can check out just fine.