0

My question is on git behavior on checkout and here are the steps to elucidate the behavior I do not understand. I make a dir testdir with files foo and bar each with a single line of text. I do the following:

git init
git add .
git commit
git branch bug

So I have the master branch and an identical bug branch. In master I add a line of text to file foo but do NOT add or commit to master, just a file change on my local tree. Next I do:

git checkout bug

I was expecting to get the following error:

   error: Your local chages to the following file would be overwritten by checkout:
     foo
  Please commit your changes or stash them before you can switch branches.

But I did NOT get the above error, and my question is how come git did not error in this case? Instead I got the following:

  root@revision-control ~/testdir# git checkout bug
  M       foo
  Switched to branch 'bug'

I am in now the bug branch but the foo file has that second line I added when I was in master.

Can anyone explain what happened here and why git let me change branches without the error message? And there are no merge conflict indicators in the file.

Next I change back to the master branch and add then commit the change to the foo file (so master file foo has 2 lines and bug foo has one line). I make a change to foo in master and add a third line of text and now when I try to checkout the bug branch (same git command as above) I get:

user@host ~/testdir# git checkout bug
error: Your local changes to the following files would be overwritten by checkout:
        foo
Please, commit your changes or stash them before you can switch branches.
Aborting

So why is this behavior now different than before and I get the error? (this is the behavior I was expecting initially).

John
  • 398
  • 1
  • 4
  • 15
  • 2
    Since the base/origin of your uncommited modification is the same in `bug` (pointing to the same commit as your current `HEAD`, `master`) Git is able to checkout the new branch. If `bug` would contain any change to that file, you'd get the expected error. – try-catch-finally Jan 31 '15 at 22:26

2 Answers2

1

According to documentation for git checkout:

git checkout <branch>
Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

Switching between branches should be treated as applying patch that represents the difference between them.

In your first case there was no difference.
In you second case there was conflict that checkout command doesn't even try to resolve.

Victor Yarema
  • 1,183
  • 13
  • 15
0

I've read that one of the solutions is to remove the files(rm) and then perform a checkout. Otherwise you could also, add and stash them. forcing the checkout like:

git checkout --force master

could be another option.

Have you tried this link GIT - Fail to checkout to other branch when nothing to commit - when file is in gitignore? Perhaps this can help you further as well.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Joey Dorrani
  • 247
  • 1
  • 1