Thread (27 messages) flat view 27 messages, 10 authors, 2016-06-15

Re: [BUG] git checkout <branch> allowed with uncommitted changes

From: Holger Hellmuth <hidden>
Date: 2016-06-15 22:52:15

On 13.10.2011 17:53, arQon wrote:
quoted
git st
# On branch foo
# Changes not staged for commit:
#   (use "git add<file>..." to update what will be committed)
#   (use "git checkout --<file>..." to discard changes in working directory)
#
#       modified:   file1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

What makes this really interesting though is this: I tried to switch to
master to see if that gave the same warning, and NOW, I get the correct
error.
quoted
git co master
error: Your local changes to the following files would be overwritten by
checkout:
         file1.txt
Please, commit your changes or stash them before you can switch branches.
Aborting
At the end of your example (in a previous email) you were on branch 
master, now in the beginning you are on foo. So you at least changed 
branch again inbetween. maybe you also committed something? Check out 
git log or gitk

I tried your example and I can checkout master and foo again and again 
and I never see the error message.
Lucky you. :P  The most likely reason for me is, I'm working on something
and I get interrupted and have to switch. Since the code may well not even
compile at this point, the last thing I want to do is commit it. git's
ability for that commit to be local is half the reason I'm trying to switch
to it. (I'm not particularly keen on having to commit broken code to even a
local repo, but that's still a hell of a lot better than having it pushed
upstream as well).
As Alexey already said, just commit and later amend. Or stash. Git 
encourages you to commit small changes you can put a name to. You never 
should delay a commit because it produces unworkable code. Instead have 
a master branch (or branches) that always compiles and branches for the 
unfinished stuff. Then it won't matter if some branch is only half working.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help