如何在 Git 中检出一个文件的特定版本?

如何在 git 中检出一个文件的特定版本?

我找到了 邮件列表上的这封邮件,上面写着:

$ git checkout HEAD~43 Makefile
$ git reset Makefile

但是我不知道如何找到“ HEAD ~ 43”,如果我做一个 git log aFile,我怎么才能找到我应该使用的“ HEAD ~ 43”?

为什么我需要为那个文件运行 git reset? 它是做什么的?

68055 次浏览

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).

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 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.

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