Cat Any File in Any Commit With Git
Ever wanted to do take a quick look at the version of some file in a different commit without checking out that commit first? Then read on, here’s how you can do it…
Goal
- Take a quick look at a special version of a file with git withou checking out the commit first
- Commit may be anything denominatable by git (commit, branch, HEAD, remote-branch)
- Branch may differ
- Pipe into another command in the shell
- Overwrite a file with an older version of itself
Tip
Syntax
git show BRANCH:PATH
Examples
-
Show the content of file
file.txt
in commita09127
:
The commit can be specified with any valid denominator and may belong to any local- or remote-branch…git show a09127a:file.txt
-
Same as above, but specify the commit relativ to the checked-out commit (handy syntax):
git show HEAD^^^^:file.txt
-
Same as above, but specify the commit relativ to the checked-out commit (readable syntax):
git show HEAD~4:file.txt
-
Same as above for a remote-branch:
git show remotes/origin/master~4:file.txt
-
Same as above for the branch
foo
in repositorybar
:git show remotes/bar/foo~4:file.txt
-
Same as above, but specify the commit relativ to the checked-out commit (handy syntax):
-
Pipe the file into another command:
git show a09127a:file.txt | wc -l
-
Overwrite the file with its version four commits ago:
git show HEAD~4:file.txt > file.txt
Explanation
If the path (aka object name) contains a colon (:
), git interprets the part before the colon as a commit and the part after it as the path in the tree, denominated by the commit.
- The commit can be specified by its reference, or the name of a local or remote branch
- The path is interpreted as absolut to the origin of the tree, denominated by the commit
- If you want to use a relative path (i.e, current directory), prepend the path accordingly — for example
./file
.
But in this case, be aware that the path is expanded against the checked-out version and not the version, that is specified before the colon!
Leave a Reply