Command Line Tricks Computers

TIL: git show <commit>:path/file.txt

Have you ever asked yourself, “How do I show a file at a particular commit or revision in its entirety?” “How do I print out a file not a diff in git?” “Show me a git file at a commit on stdout!” Here’s how you do it.

I knew about git show <commit> which shows a diff of all the files changed in a commit. You can even scope it with git show <commit> path/file.txt to show the diff for a particular file. Today I learned if you use a : (colon) instead of a (space) between the commit id and the file path git will instead show you the file in its entirety at that revision.

$ cd temp/
$ mkdir git_show_example
$ cd git_show_example/
$ git init
Initialized empty Git repository in .../temp/git_show_example/.git/
$ commit --allow-empty -m 'Initial commit'
[master (root-commit) 2f2f99d] Initial commit
$ echo 'Hello, World' > file.txt
$ git add file.txt
$ git commit -m 'Hello world'
[master 4bc4156] Hello world
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
$ echo 'Goodbye' > file.txt
$ git commit -a -m 'Goodbye'
[master 8af3b90] Goodbye
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git show 4bc4156:file.txt
Hello, World