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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1173676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:47:03  来源:igfitidea点击:

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

git

提问by n179911

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 resetfor that file? What does it do?

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

回答by Fake Code Monkey Rashid

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

用于当您想从某个范围内获取文件的版本时(我这样做是为了怀旧)。

回答by dhill

HEAD~43is 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

回答by Jim Puls

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

HEAD~43指的是文件的提交(版本)。取而代之的是,您可以使用从git log文件中获得的提交哈希。如果您只需要该文件,则无需git reset在其上运行;仅当您要将文件转发到当前 HEAD 时才需要这样做。