乐闻世界logo
搜索文章和话题

How can i view an old version of a file with git?

3个答案

1
2
3

If the commit occurred within the past 90 days, executing this command by date appears as follows:

bash
git show HEAD@{2013-02-25}:./fileInCurrentDirectory.txt

Note that HEAD@{2013-02-25} refers to the HEAD being at 2013-02-25 in this repository (using reflog), not the last commit before 2013-02-25 in the branch history.

This means that by default, this method only applies to history within the last 90 days. Otherwise, you need to execute the following:

bash
git show $(git rev-list -1 --before="2013-02-26" HEAD):./fileInCurrentDirectory.txt
2024年6月29日 12:07 回复

Quickly view differences between the file and old versions:

git show -1 filename.txt -> Compare with the HEAD revision

git show -2 filename.txt -> Compare with the second-to-last revision

git show -3 filename.txt -> Compare with the third-to-last revision

2024年6月29日 12:07 回复

You can use the git show command with the repository root path (using relative paths like ./ or ../):

shell
$ git show REVISION:path/to/file

Replace REVISION with your actual revision (such as a Git commit SHA, tag name, branch name, relative commit name, or any other identifier for a commit in Git). For example, to view the file version from 4 commits ago, use:

shell
$ git show HEAD~4:src/main.c

Even when using paths relative to the current directory, Windows Git requires forward slashes.

2024年6月29日 12:07 回复

你的答案