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

Re: Suggestion: make git checkout safer

From
Jeff King <peff@peff.net>
Date
Jun 3, 2015, 09:35 UTC
Message-ID
<20150603093514.GF32000@peff.net>
In-Reply-To
<loom.20150603T110826-777@post.gmane.org>
On Wed, Jun 03, 2015 at 09:21:59AM +0000, Ed Avis wrote:
> I had expected that 'git checkout .' would fix up my working tree to make it
> match the repository (in this case, the current revision of the master
> branch).
It did. :)
Show 12 quoted lines
> The user interface might be something like:
> 
> % git checkout .
> error: Your local changes to the following files would be overwritten:
>         foo
> You may want to commit or stash these changes, or delete the files if you
> don't want them.  Use 'git checkout --force' to proceed, throwing away
> local changes.
> Aborting
> 
> If the checkout operation would only involve creating some files on disk
> which aren't currently there, then it would proceed without prompting.

Thanks for explaining. I see where you are coming from, though I'm still a bit lukewarm on the idea, if only because the vast majority of invocations would involve "--force".

It also seems a bit special-cased to treat restoring deletions specially. I would say the more "usual" way to use checkout like this is to give specific paths. I.e., run "git status", say "oh, I need to restore the contents of 'foo', but not 'bar'", and run "git checkout foo". That works regardless of the type of change to "foo" and "bar".

If we want to introduce more safety here, I'd be inclined to perform the operation by default, but give a better escape hatch. For example, by creating a loose object for any file we're about to overwrite, and possibly writing an entry into a log. That's a lot more work, but has a few advantages:

  1. It helps even when you just ran with "--force" followed by an
     "oops, why did I do that?" moment.
  2. It can help other commands like "git clean".
  3. That log could form a basis for a "git undo" program to help with
     "oops" moments in general (e.g., if you use "git reset ." to
     overwrite what is in the index, we have all of the old file content
     in objects, but it can sometimes be a pain to figure out _which_
     objects went where.
-Peff
Previous: Ed AvisNext: Ed Avis
Message 4 of 28 in “Suggestion: make git checkout safer”
  1. Ed AvisJun 3, 2015
  2. Jeff KingJun 3, 2015
  3. Ed AvisJun 3, 2015
  4. Jeff KingJun 3, 2015
  5. Ed AvisJun 3, 2015
  6. Junio C HamanoJun 3, 2015
  7. Randall S. BeckerJun 3, 2015
  8. Junio C HamanoJun 3, 2015
  9. Randall S. BeckerJun 3, 2015
  10. Stefan BellerJun 3, 2015
  11. Ed AvisJun 4, 2015
  12. Ed AvisJun 4, 2015
  13. Torsten BögershausenJun 3, 2015
  14. Kevin DaudtJun 3, 2015
  15. Ed AvisJun 4, 2015
  16. Torsten BögershausenJun 4, 2015
  17. Ed AvisJun 5, 2015
  18. Duy NguyenJun 5, 2015
  19. Eric SunshineJun 5, 2015
  20. Junio C HamanoJun 5, 2015
  21. Ed AvisJun 5, 2015
  22. Eric SunshineJun 5, 2015
  23. Philip OakleyJun 3, 2015
  24. Junio C HamanoJun 3, 2015
  25. Jeff KingJun 3, 2015
  26. Randall S. BeckerJun 3, 2015
  27. Junio C HamanoJun 3, 2015
  28. John SzakmeisterJun 4, 2015

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.