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
$ 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 <email@example.com> Date: XXX Deleted this file. Ha ha ha! commit beforeCommit Author: File Changer <firstname.lastname@example.org> Date: XXX Added a new file at file/that/was/deleted
git show to view the version before it was deleted:
$ git show beforeCommit:file/that/was/deleted