Pluck out an old revision of a file with git show

by Kenneth Kalmer on March 20, 2009

I was busy merging work back and forth between topic branches today and by accident miss-merged a 700-line spec. Everything else was fine (or so I hope) except this one file. I couldn’t use git revert, since that would mean I’ll have to redo all the merges I made.

So in panic I googled and read several man pages, coming to the following conclusion that worked quite well:

  1. Find the offending commit with git log or git log -p, not the revision number prior to the catastrophic change.
  2. View the file at that point in time with git show SHA-ID:path/to/file
  3. Replace current copy with git show SHA-ID:path/to/file > path/to/file
  4. Verify the damage has been undone with git diff
  5. Commit
  6. Breathe

Git is a double edged sword, it is extremely powerful and useful, but can be a real pain in the behind. I love git, and will not easily be convinced to move to something else.

3 comments

Thanks for this, nice to have another get out of jail free card when I need it

by Andrew Timberlake on March 20, 2009 at 4:33 pm. #

Yip, blogged it so I can’t loose it :)

by Kenneth Kalmer on March 20, 2009 at 5:08 pm. #

One important thing to note is that the path to the file specified has to be the path relative to the root of the git repository. It can’t be a relative path :(

Seems silly to me, but there you have it.

by Josh on November 3, 2009 at 12:23 am. #

Leave your comment

Required.

Required. Not published.

If you have one.