Skip to content

Instantly share code, notes, and snippets.

@TheCodeArtist
Last active February 3, 2023 21:05
Show Gist options
  • Save TheCodeArtist/a90978ebca0ff6743036 to your computer and use it in GitHub Desktop.
Save TheCodeArtist/a90978ebca0ff6743036 to your computer and use it in GitHub Desktop.
git checkout a previous revision of a specific file
Start by adding the following alias to your gitconfig.
[alias]
prevision = "!f() { git checkout `git log --oneline $2 | awk -v commit="$1" 'FNR == -commit+1 {print $1}'` $2;} ;f"
Usage:
$ git prevision <N> <filename>
will checkout the <N>th revision (counting backwards from HEAD) into the history of the file <filename>.
For example to checkout the immediate previous version of a file x/y/z.c use
$ git prevision -1 x/y/z.c
Have fun!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment