如何在Git中查看一个文件的特定版本?

时间:2021-11-29 19:28:54

How can I check out a particular version of one file in git?

如何在git中查看一个文件的特定版本?

I found this mail on the mailing list, which said:

我在邮件列表上发现了这封邮件,其中说:

$ git checkout HEAD~43 Makefile
$ git reset Makefile

But I don't understand how to find out 'HEAD~43', if I do a git log aFile, how can I find out which 'HEAD~43' I should use?

但是我不明白如何找出'HEAD~43',如果我做一个git log aFile,怎么才能找出我应该使用哪个'HEAD~43'?

And why do I need to run git reset for that file? What does it do?

为什么我需要为该文件运行git reset?它有什么作用?

3 个解决方案

#1


You know what commit (ie: the specific revision) the file belongs to? Then do:

您知道该文件属于哪个提交(即:特定修订)?然后做:

git checkout <commit> <file>

The other command:

另一个命令:

git checkout HEAD~N <file>

Is for when you want to get a version of the file from a range back (which I do for nostalgia).

当你想从一个范围后面获取该文件的版本时(我为怀旧而做)。

#2


HEAD~43 is just treeish, so you can use a hash or a tag. You have to separate treeish from the filename with --, otherwise it is treated as filename. For example.

HEAD~43只是树状,所以你可以使用哈希或标签。您必须使用 - 将文件名与文件名分开,否则将其视为文件名。例如。

git checkout v0.45 -- filename
git checkout HEAD^ -- filename
git checkout 16bb1a4eeaa9 -- filename

#3


HEAD~43 refers to the commit (version) of the file. Instead of that, you can use the commit hash you get from doing git log on the file. If you just want the file, you don't need to run git reset on it; that's only necessary if you want to forward-port the file to the current HEAD.

HEAD~43指的是文件的提交(版本)。而不是那样,你可以使用你在文件上执行git log得到的提交哈希。如果你只是想要文件,你不需要在它上面运行git reset;只有当你想将文件转发到当前的HEAD时才需要。

#1


You know what commit (ie: the specific revision) the file belongs to? Then do:

您知道该文件属于哪个提交(即:特定修订)?然后做:

git checkout <commit> <file>

The other command:

另一个命令:

git checkout HEAD~N <file>

Is for when you want to get a version of the file from a range back (which I do for nostalgia).

当你想从一个范围后面获取该文件的版本时(我为怀旧而做)。

#2


HEAD~43 is just treeish, so you can use a hash or a tag. You have to separate treeish from the filename with --, otherwise it is treated as filename. For example.

HEAD~43只是树状,所以你可以使用哈希或标签。您必须使用 - 将文件名与文件名分开,否则将其视为文件名。例如。

git checkout v0.45 -- filename
git checkout HEAD^ -- filename
git checkout 16bb1a4eeaa9 -- filename

#3


HEAD~43 refers to the commit (version) of the file. Instead of that, you can use the commit hash you get from doing git log on the file. If you just want the file, you don't need to run git reset on it; that's only necessary if you want to forward-port the file to the current HEAD.

HEAD~43指的是文件的提交(版本)。而不是那样,你可以使用你在文件上执行git log得到的提交哈希。如果你只是想要文件,你不需要在它上面运行git reset;只有当你想将文件转发到当前的HEAD时才需要。