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

Re: Restore a single file in the index back to HEAD

From
Shawn Pearce <spearce@spearce.org>
Date
Oct 27, 2006, 08:15 UTC
Message-ID
<20061027081545.GF29057@spearce.org>
In-Reply-To
<4541BE8E.5050605@op5.se>
Andreas Ericsson <ae@op5.se> wrote:
Show 27 quoted lines
> Shawn Pearce wrote:
> >Andy Parkins <andyparkins@gmail.com> wrote:
> >>However, it led me to wonder what the inverse of git-update-index is.
> >
> >git-update-index  :-)
> >
> >You can use something like:
> >
> >    git ls-tree HEAD oops/file1 | git update-index --index-info 
> >
> >to restore the index state of oops/file1.
> >
> >
> >Which leads us to the always interesting, fun and exciting:
> >
> >    git ls-tree -r HEAD | git update-index --index-info 
> >
> >which will undo everything except 'git add' from the index, as
> >ls-tree -r is listing everything in the last commit.
> >
> 
> ... and also shows The Power of the Pipe, which Daniel@google was 
> missing in recent versions of git. ;-)
> 
> Btw, this is most definitely not a documented thing and requires a bit 
> of core git knowledge, so perhaps the "shell-scripts were good for 
> hackers to learn what to pipe where" really *is* a very important point.
Agreed.

I learned that trick while studying the update-index source code and tried to wrap my tiny little head around the various formats --index-info accepts and how that code automatically guesses the correct format. :-)

Though I have to admit I wipped up a little test repository just to make sure what I was writing in the email worked properly; I can't say I've done it myself too many times in the past...

Previous: Andreas EricssonNext: Alex Riesen
Message 7 of 31 in “Restore a single file in the index back to HEAD”
  1. Andy ParkinsOct 26, 2006
  2. Alex RiesenOct 26, 2006
  3. Andy ParkinsOct 27, 2006
  4. Shawn PearceOct 27, 2006
  5. Andy ParkinsOct 27, 2006
  6. Andreas EricssonOct 27, 2006
  7. Shawn PearceOct 27, 2006
  8. Alex RiesenOct 27, 2006
  9. Andreas EricssonOct 27, 2006
  10. Junio C HamanoOct 27, 2006
  11. Luben TuikovOct 27, 2006
  12. Nguyen Thai Ngoc DuyNov 1, 2006
  13. Junio C HamanoNov 1, 2006
  14. Junio C HamanoNov 1, 2006
  15. Nguyen Thai Ngoc DuyNov 1, 2006
  16. Jakub NarebskiNov 1, 2006
  17. Jakub NarebskiNov 1, 2006
  18. Andy ParkinsNov 1, 2006
  19. Junio C HamanoNov 1, 2006
  20. Andy ParkinsNov 1, 2006
  21. Junio C HamanoNov 1, 2006
  22. Andy ParkinsNov 1, 2006
  23. Junio C HamanoNov 1, 2006
  24. Andy ParkinsNov 1, 2006
  25. Junio C HamanoNov 1, 2006
  26. Andy ParkinsNov 1, 2006
  27. Junio C HamanoNov 1, 2006
  28. Andy ParkinsNov 2, 2006
  29. Robin RosenbergNov 1, 2006
  30. Salikh ZakirovNov 2, 2006
  31. Added description for inverting git-update-index using --index-infoAndy Parkins, Oct 27, 2006

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.