2

I cloned a repository a few days back and to make the cod run, I needed to modify the class path. Today I was told to pull X branch which I did using git pull.

Now when I do git checkout X, I get an warning for my classpath file

Stash or commit it to prevent local changes from being overwritten.

To recreate this warning, I tried two scenarios in other projects.

1st Scenario

In a git repo. Make changes in a file and not commit it. Then create a new branch and checkout to it. In that case it does not throw this warning.

2nd Scenario

In another repo,

  1. Commit a new file.
  2. Create a branch, checkout to that branch and make changes to that files and commit it
  3. Switch back to master, make local changes and not commit it. Then if I invoke git checkout X it throws that warning.

I don't really know what is the logic behind git showing this warning? Obviously, I can see these two scenarios and notice that they give different results. But I don't know, what git looks for to throw this warning.

Could someone please explain this to me and how this explains the warning I saw with my classpath file?

Florian Neumann
  • 5,587
  • 1
  • 39
  • 48
Max
  • 9,100
  • 25
  • 72
  • 109

2 Answers2

3

Each time you checkout to another branch, it will reset the working tree to match the index of that new branch. And the index is set to the new branch HEAD.

If that reset involves a modified (tracked) file, the checkout will abort with the warning you see.

Your first scenario creates a branch from an existing one: its index will be the same than the existing one (including the current modifications) because HEAD doesn't move the HEAD of the new branch is the same as the one you were before): you can switch to it without having to reset anything: the working tree is unchanged.

Your second scenario involve two different indexes for two different branches with two different HEAD: you cannot switch to one without stashing or committing your changes from your current branch, because the checkout needs to reset the working tree to that other branch content (HEAD), which would discard (and lose) your changes.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @Thor yes, and my point is that a checkout will **reset the working tree** to the index of the new branch when you switch branches. And that index is set to the new branch HEAD. So the files being modified would be overwritten in order to match the branch HEAD (with an index reflecting that branch HEAD). – VonC May 14 '14 at 09:10
  • Ok, for some reason I am not getting it. Let me ask it like this, in 2nd scenario at time of checkout, we have nothing in index and one tracked modified uncommitted file(in master), and we have nothing in index and one file in working tree in (X branch).Head is branch is one ahead of Head in master. So, when I git checkout X,what is reset to what and what causes the warning ? Thanks for your help. – Max May 14 '14 at 10:16
  • @Thor when you checkout (second scenario) your changes would be lost because the tracked modified file would be reset to the HEAD content of the second branch. – VonC May 14 '14 at 10:18
0

Basically, the problem is that when if you try to switch branches (which is what git checkout X is doing), and the target branch (X in this case) has made changes to a file that you've also modified locally but haven't committed, the local changes could be overwritten.

Remember: when you change branches, the files in your working tree change to reflect the new index.

So if the you've modified a file that needs to be changed in order to match the target index, your changes would be overwritten, so the checkout aborts.

NHDaly
  • 7,390
  • 4
  • 40
  • 45