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