git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Michael J Gruber <git@drmicha.warpmail.net>
Date
Oct 13, 2011, 15:09 UTC
Message-ID
<4E96FF3D.90600@drmicha.warpmail.net>
In-Reply-To
<loom.20111013T094053-111@post.gmane.org>
arQon venit, vidit, dixit 13.10.2011 10:40:
Show 44 quoted lines
> Which, as you'd expect, results in both the on-disk copies and other branches
> becoming corrupted.
> 
> Tested on git versions 1.7.6 and 1.7.7 (msysgit)
> 
> http://benno.id.au/blog/2011/10/01/git-recursive-merge-broken describes
> something that sounds similar, but that's supposedly fixed on 1.7.7,
> whereas this happens on that as well.
> 
> master is a tracking branch, "ttfcon" is the branch I was using to develop
> a change. Got to a good point on the branch, merged it in:
> 
> $ git co master
> $ git merge ttfcon
> Updating b9f0c75..6280b7a
> Fast-forward
>  .gitignore                |    2 ++
>  code/renderer/tr_font.cpp |   27 ++++++++-------------------
>  2 files changed, 10 insertions(+), 19 deletions(-)
> 
> $ git st
> # On branch master
> # Your branch is ahead of 'origin/master' by 3 commits.
> 
> back to the branch to mess around with a couple of things to be sure this
> is what i want to push
> $ git co ttfcon
> do stuff
> 
> $ git st
> # On branch ttfcon
> # 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:   code/freetype-2.3.11/builds/win32/visualc/freetype.vcproj
> #       modified:   code/renderer/tr_font.cpp
> 
> so far so good...
> 
> $ git ci -m "blah" code/freetype-2.3.11/builds/win32/visualc/freetype.vcproj
>  1 files changed, 4 insertions(+), 0 deletions(-)
> 
> note that tr_font is locally modified and still *not committed* at this point.
and neither staged for commit, exactly.
Show 7 quoted lines
> $ git co master
> M       code/renderer/tr_font.cpp
> Switched to branch 'master'
> Your branch is ahead of 'origin/master' by 3 commits.
> 
> boom. instead of rejecting the branch change, git switches branches anyway,
> and doesn't do anything about the uncommitted changes in the file itself -

Exactly. git leaves them as they are, without changing what you have in your work tree.

(This is possible because the switch ttfcon to master involves no changes which conflict with the chnage that you have in your work tree).

> meaning they're now effectively "in" master because they're still on disk,
> so now the master is poisoned.

Not at all. They are on "disk" (work tree). Full stop. Not staged, not committed, not at all "in master".

Show 9 quoted lines
> 
> "git st" does show the change:
> 
> # On branch master
> # Changes not staged for commit:
> #       modified:   code/renderer/tr_font.cpp
> 
> but it's a change I never MADE on this branch (ie master), only on the
> other branch.

You never made it on the other branch either. You made it in the work tree. And "git status" clearly says so: modified, not staged.

Show 6 quoted lines
> "git diff" is just as confused as I am:
> 
> $ git diff ttfcon
> --- a/code/renderer/tr_font.cpp
> +++ b/code/renderer/tr_font.cpp
> +		// git branch bug

"git diff" shows you the change you have in your work tree, i.e. the difference between index (which coincides with master since nothing is staged) and work tree. The fact that there is a difference is equivalent to saying "there are unstaged changes".

> So it's picking up the difference between the two branches, but as far as

No. The difference between the branches is the change to freetype.vcproj because you committed that to ttfcon, not master.

> the *actual file* goes, master now has a line in it that shouldn't be there.
It's in the work tree, not master....
Show 7 quoted lines
> I'm just trying out git as a possible replacement for SVN, so maybe I'm
> mistaken about what "should" happen, but AIUI git switching branches with
> uncommitted changes is a bug (and given that it poisoned a branch that I
> wasn't on, it certainly looks like one). A couple of days ago it DID complain
> when I tried to switch with uncommitted files still present, so it was working
> properly then. I have no idea what's made it happy to ignore them now:
> nothing's changed that I know of.

When switching branches, git tries to preserves the changes that you have in your work tree. If it is possible (because there is no overlap, as written above), it hapilly does just that. If not it barks.

I think you have to wrap your head around the Git model after unwinding it from the svn model, which is normal ;)

Cheers, Michael

Previous: Victor Engmark
Message 36 of 36 in “[BUG] git checkout <branch> allowed with uncommitted changes”
  1. arQonOct 13, 2011
  2. Nguyen Thai Ngoc DuyOct 13, 2011
  3. Alexey ShumkinOct 13, 2011
  4. arQonOct 13, 2011
  5. Andreas EricssonOct 13, 2011
  6. arQonOct 13, 2011
  7. Carlos Martín NietoOct 13, 2011
  8. arQonOct 13, 2011
  9. Alexey ShumkinOct 13, 2011
  10. Jakub NarebskiOct 13, 2011
  11. arQonOct 13, 2011
  12. Carlos Martín NietoOct 13, 2011
  13. arQonOct 13, 2011
  14. Alexey ShumkinOct 13, 2011
  15. Alexey ShumkinOct 14, 2011
  16. Holger HellmuthOct 13, 2011
  17. Carlos Martín NietoOct 13, 2011
  18. arQonOct 13, 2011
  19. Junio C HamanoOct 13, 2011
  20. arQonOct 13, 2011
  21. Jeff KingOct 14, 2011
  22. Holger HellmuthOct 14, 2011
  23. Victor EngmarkOct 14, 2011
  24. arQonOct 16, 2011
  25. Junio C HamanoOct 16, 2011
  26. Holger HellmuthOct 16, 2011
  27. Carlos Martín NietoOct 13, 2011
  28. Sergei OrganovOct 13, 2011
  29. PJ WeisbergOct 13, 2011
  30. Holger HellmuthOct 13, 2011
  31. arQonOct 13, 2011
  32. Holger HellmuthOct 13, 2011
  33. Victor EngmarkOct 13, 2011
  34. arQonOct 13, 2011
  35. Victor EngmarkOct 14, 2011
  36. Michael J GruberOct 13, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.