Checking out old versions of files in git

15 May, 2015

Here’s another useful alias if you’re using git. Sometimes you want to recover a file which has been deleted, or restore an old version of a single file. Or, you might want to grab a file from a different branch. This alias will let you do that:

    get-file = "!f() { git show $1:$2 > $2; }; f"

Use it like this:

# Get the version of from commit 456e17b
$ git get-file 456e17b
# Get the version of foo.cpp from the 'stable' branch
$ git get-file stable path/to/foo.cpp

Note that this will write the file out to its actual location (overwriting whatever’s there), so don’t run it if you have local modifications to that file, unless you don’t mind those modifications getting overwritten!

If you are using git-media, or something similar, you need to pipe the file through the filter. I set up another alias to do this:

    get-media = "!f() { git show $1:$2 | git-media filter-smudge > $2; }; f"
Tagged with: git, alias