tags:

views:

32

answers:

2

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?

A: 

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

Stein G. Strindhaug
It's not just getting a conflict, it's having to perform a merge at all on any files.
Jefromi
Ah, I think I see what you meant - having to merge a given file creates the potential for conflicts. I think that the philosophy of the decision is more about making sure the meaning of your diffs doesn't change, though - that phrase "your modifications in modifications in context" is key.
Jefromi
+4  A: 

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.

Jefromi
Also if you added D to `master` but not to `dev`, it will probably allow you to switch to dev even if you have local changes to D, but when you switch back to master, it will refuse (am I wrong?)
hasen j
@hasen: I don't think it'll let you do that either - as part of switching to dev, it wants to remove that file, and it can't, because there are modifications.
Jefromi
I have file a.txt and b.txt in both master and experiment branch. I am in master and edited a.txt. now master has unstaged local changes in a.txt and I am able to git co experiment.
Nadal
@Jefromi why would it remove that file?
hasen j
@dorelal: yes, a.txt is in both branches, but it is not in the diff between the two; that is, the same version of a.txt is in both.
Jefromi
@hasen j: When you switch from a branch containing a file to one not containing a file, git removes it. That's normal operation.
Jefromi
@Jefromi yes a.txt is in both the branches. Then I got in master branch and changed something in a.txt . now if I try to do git co experiment , I am able to do so. I though your answer was I should not be allowed.
Nadal
@doreal: As I said in my previous comment to you, git will only stop you if there is a difference between the versions of a.txt in the two branches. Run `git diff master experimental a.txt`. Are there differences? With local (uncommitted) modifications to a.txt, you should be able to check out the other branch if and only if there are no differences.
Jefromi
got it this time. thanks for your patience.
Nadal