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

Re: git checkout no longer warning about uncommitted/untracked files!

From
Jeff King <peff@peff.net>
Date
May 23, 2011, 21:18 UTC
Message-ID
<20110523211826.GD6281@sigill.intra.peff.net>
In-Reply-To
<1306184704408-6396225.post@n2.nabble.com>
On Mon, May 23, 2011 at 02:05:04PM -0700, funeeldy wrote:
> This is with version 1.7.5.2 - it just keeps the files as modified when I
> checkout a different branch.
Right. This has always been the way git works.
> The bad thing about this is that if I make a change on both branches,
> only the last change is kept, silently.

No, in the example below, you don't actually make a change on a branch. You make a change to your _worktree_, but that change is not associated with a branch until you actually commit (which you never do in your example).

Show 10 quoted lines
> private/marlene_cote/test/anroot/projects/kernel main_int $ git st
> # On branch main_int
> # Changed but not updated:
> #   (use "git add <file>..." to update what will be committed)
> #   (use "git checkout -- <file>..." to discard changes in working
> directory)
> #
> #	modified:   Makefile
> #
> no changes added to commit (use "git add" and/or "git commit -a")
OK, so you have a change to your Makefile...
Show 5 quoted lines
> /private/marlene_cote/test/anroot/projects/kernel main_int $ git checkout
> main
> M	Makefile
> Branch main set up to track remote branch main from origin.
> Switched to a new branch 'main'

And now you switch to another branch. Because the version of Makefile in "main" is the same as the one on "main_int", git can switch branches and preserve your worktree change (if there had been a change to Makefile in main_int, you would have seen an error, and git would have told you to commit or stash your change sfirst).

> /private/marlene_cote/test/anroot/projects/kernel main $ git checkout
> main_int
> M	Makefile
> Switched to branch 'main_int'

Now you're back on main_int, but again, you still have the same change in Makefile in your worktree.

> /private/marlene_cote/test/anroot/projects/kernel main_int $ git checkout
> main
> M	Makefile
> Switched to branch 'main'
And back to main, with the same change in the worktree.
> v/private/marlene_cote/test/anroot/projects/kernel main $ vi Makefile 

And now you edit the Makefile to new content. If you are expecting the intermediate state of the Makefile to be kept, it is not. You never committed it.

> /private/marlene_cote/test/anroot/projects/kernel main $ git checkout
> main_int
> M	Makefile
> Switched to branch 'main_int'

And you still have the new content, because none of these checkouts is changing your Makefile at all; you are just switching other files around while the same changes to Makefile sit in the worktree.

-Peff
Previous: Marlene Cote
Message 8 of 8 in “git checkout no longer warning about uncommitted/untracked files!”
  1. funeeldyMay 23, 2011
  2. René ScharfeMay 23, 2011
  3. Jeff KingMay 23, 2011
  4. funeeldyMay 23, 2011
  5. funeeldyMay 23, 2011
  6. Jeff KingMay 23, 2011
  7. Marlene CoteMay 23, 2011
  8. Jeff KingMay 23, 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.