A comment from HappyGiraffe on the previous post pointed out that the “show” verb would accept the commit hash along with the filename to return the same result I was looking. I had actually tried this out originally but ran into the problem where I couldn’t get the syntax correct. After re-reading the man page for the show verb, it turns out I needed to specify the full path for the filename relative to the top of the repository tree:
git show commit_id:some/path/to/file
The problem becomes getting the full path to the file in the repository. Here we really get to see my inexperience with Git and HappyGiraffe came to the rescue again. He pointed out that the “ls-file” verb should do the trick. After a glance at the man page, it turns out the syntax to get the full path for a file should look like this:
git ls-file –full-name file
So now rather than digging out the blob hash id from the commit, we can just specify the commit id and the full path of the file. To get the commit id from git, it’s a matter of dumping the log and doing some awk work.
But I got curious about whether it was possible to dump just the commit has for a log entry and it turns out that you can using the following syntax:
git log -1 –pretty=format:%H file
The “-1” argument only displays the last log entry for that particular file. The “pretty” option specifies that we’re going to format the output in a paticular way and the “%H” argument to it returns the full hash (a lower case “h” only returns a partial on the hash id). Now we don’t have to pipe it to awk for any string hackery.
So the end result becomes (in pseudo shell script):
commit_id = git log -1 –pretty=format:%H file
fullpath = git ls-file –full-name file
git show commit_id:fullpath
Which is excellent. Though… it’d still be nice if the folks maintaining git would provide us with a “cat” verb.