How can I view an old version of a file with Git?
Is there a command in Git to see (either dumped to stdout, or in $PAGER
or $EDITOR
) a particular version of a particular file?
You can use git show
with a path from the root of the repository (./
or ../
for relative pathing):
$ git show REVISION:path/to/file
Replace REVISION
with your actual revision (could be a Git commit SHA, a tag name, a branch name, a relative commit name, or any other way of identifying a commit in Git)
For example, to view the version of file <repository-root>/src/main.c
from 4 commits ago, use:
$ git show HEAD~4:src/main.c
Git for Windows requires forward slashes even in paths relative to the current directory. For more information, check out the man page for git-show
.
Doing this by date looks like this if the commit happened within the last 90 days:
git show HEAD@{2013-02-25}:./fileInCurrentDirectory.txt
Note that HEAD@{2013-02-25}
means "where HEAD was on 2013-02-25" in this repository (using the reflog), not "the last commit before 2013-02-25 in this branch in history".
This is important! It means that, by default, this method only works for history within the last 90 days. Otherwise, you need to do this:
git show $(git rev-list -1 --before="2013-02-26" HEAD):./fileInCurrentDirectory.txt