9 April 2021

Showing A File At A Specific Git Revision

Here is a technique for showing the version of a file at a specific Git revision. It’s not a novel technique — there’s naturally a Stack Overflow answer for explaining this process — but I constantly forgetting how to do this and end up having to search for it. So I’m including it here just so I know where to go when I need to look this up again.

To display the contents of a file at a given revision, run the following command:

$ git show <revision>:<filename>

For example, to view the version of README.md” on the dev branch:

$ git show dev:README.md

There is an alternative form of this command that will show the changes applied to that file as part of the commit:

$ git show <revision> -- <filename>

This can be used alongside the log command to work out what happened to a file that was deleted.

First, view the history of the file. You are interested in the ID before the commit that deleted the file: attempting to run git show using the deletion commit ID it will result in nothing being shown.

$ git log -- file/that/was/deleted
commit abc123
Author: The Deleter <deleter@example.com>
Date:   XXX

    Deleted this file.  Ha ha ha!

commit beforeCommit
Author: File Changer <changer@example.com>
Date:   XXX

    Added a new file at file/that/was/deleted

Then, use git show to view the version before it was deleted:

$ git show beforeCommit:file/that/was/deleted

Techniques


Previous post
On Google and Winer's Rule One Here’s another blog post about Google, because, why not? It all started when I read a post from Dave Winer, in which he talks about a principal of
Next post
Jumping to the Root of a Git Repo Sometimes, when you’re browsing your social feeds, you come across a tool or technique for doing something that you think is good to know, but you