Pluck out an old revision of a file with git show

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.

Flattr this!

Welcome to the Open Sourcery Archives. These are my older blog posts, from days gone by. I'm keeping them up as part of the historical record. That, and I'm strangely sentimental about them. Please keep in mind that things most certainly have changed since these articles were written, links to external sites might be broken, and general thinking might have changed.

Regardless of all this, I hope you enjoy your stay!

comments powered by Disqus