如何在 git 存储库中获取旧版本文件的副本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14995506/
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 15:33:51  来源:igfitidea点击:

How to get a copy of an older version of a file in a git repository?

gitgit-checkout

提问by jbranchaud

I have a version of a .texfile from a number of commits ago that I would like to get a copy of. I have the sha1 hash value for the commit that has the version of that file that I want. I do not want to replace the current version of the file. Rather, I want to just get a separate copy of it that reflects its state at the older version.

我有一个.tex来自多次提交的文件版本,我想获取它的副本。我有提交的 sha1 哈希值,该提交具有我想要的该文件的版本。我不想替换文件的当前版本。相反,我只想获得它的单独副本,以反映其在旧版本中的状态。

A lot of similar questions suggest using git checkout <sha1> -- file.tex, but this just keeps giving "error: pathspec 'file.tex' did not match any file(s) known to git."

许多类似的问题建议使用git checkout <sha1> -- file.tex,但这只是不断给出“错误:pathspec 'file.tex' 与 git 已知的任何文件不匹配。”

The file I am interested in originally existed in the top-level directory of the repository. I am currently in a sub-directory of the repository trying to run this command so as to get the older version of file.tex in the subdirectory.

我感兴趣的文件最初存在于存储库的顶级目录中。我目前在存储库的子目录中尝试运行此命令,以便在子目录中获取旧版本的 file.tex。

Is this possible? How can I do this?

这可能吗?我怎样才能做到这一点?

回答by qqx

You can use git cat-fileto dump the contents of the file to the standard output and redirect that into your desired destination:

您可以使用git cat-file将文件内容转储到标准输出并将其重定向到您想要的目的地:

git cat-file -p <sha1>:./file.tex > wherever.tex

The ./is necessary if you are in a subdirectory of the repository, if you're in the top-level of the repository it may be omitted. Also, that may not work in older versions of git, in which case you'd need to explicitly supply the full path to the file relative to the repository's root.

./如果您位于存储库的子目录中,则这是必需的,如果您位于存储库的顶层,则可以省略它。此外,这可能不适用于旧版本的 git,在这种情况下,您需要明确提供相对于存储库根目录的文件的完整路径。

回答by William Seiti Mizuta

I think that the best solution is to overwrite temporally your file. In your top-level of your repository:

我认为最好的解决方案是暂时覆盖您的文件。在您的存储库的顶级:

git checkout <sha1> file.tex
cp file.tex directory
git checkout HEAD file.tex

回答by gws

Use git showwith absolute repository paths:

使用git show具有绝对的版本库路径:

workdir$ git show revision:repo/module/package/code.file > code.file.old

workdir$ git show 修订:repo/module/package/code.file > code.file.old

or paths relative to the current directory:

或相对于当前目录的路径:

package$ git show revision:./code.file > workdir/code.file.old

package$ git show 修订:./code.file > workdir/code.file.old

In contrast to checking out a file, showdoes not intrinsically change your workspace, you can do anything you like with the output.

与签出文件相反,show它本质上不会改变您的工作区,您可以对输出执行任何您喜欢的操作。

Credit to this answer, and of course see full details in the manual.

归功于此答案,当然,请参阅手册中的完整详细信息。