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

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

From
Nguyen Thai Ngoc Duy <pclouds@gmail.com>
Date
Oct 13, 2011, 10:48 UTC
Message-ID
<CACsJy8Dzy5-kOZAjwdx=ooUdnN0L2F3EiNQ7b==3AGQZYjEUXQ@mail.gmail.com>
In-Reply-To
<loom.20111013T094053-111@post.gmane.org>
On Thu, Oct 13, 2011 at 7:40 PM, arQon <arqon@gmx.com> wrote:
> $ git co master
> M       code/renderer/tr_font.cpp
> Switched to branch 'master'
> Your branch is ahead of 'origin/master' by 3 commits.
...
> At this point, reverting the master with "checkout --" also wipes out the
> changes on the other branch. It's like the merge symlinked the two branches
> rather than, well, merging them.

It does show you that there are changes in the working tree and you could have switched back with "git co -", done whatever you want with your changes then switched to master again.

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

git tries to keep all changes on working tree you have. If you have changes in file A and the new branch changes in file B, fine. If the new branch also changes in file A too, it'll complain because otherwise it may overwrite your changes. What it actual does is "Two way merge", there is a table in "git read-tree" man page that describes exactly how it is done, what cases would fail...

I see it as more choices. As I said above, it does tell you there are changes and you could do something. You could make alias "co" that check for worktree/index cleanliness before calling checkout. Something like this maybe (I have not tested it)

git config alias.co '!git update-index --refresh && git diff-files --quiet && git diff-index --cached --quiet HEAD && git checkout "$@"'

A config key to enforce this may be nice. I don't know, I have never had problems with current behavior.

-- 
Duy
Previous: arQonNext: Alexey Shumkin
Message 2 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.