开发者

git checkout experiment is allowed sometimes and sometimes not if I have local changes

I have a branch called experiment.

git checkout master
echo 'some changes' > a.tx开发者_开发问答t
# 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:

When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context.

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 -m option 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.


I'm not quite sure how git does this, but it tries to prevent you from accidentally overwrite uncommitted changes. If it decides you could get a conflict it will not allow it, I think...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜