如何在git存储库中获取旧版本文件的副本? [英] How to get a copy of an older version of a file in a git repository?

查看:166
本文介绍了如何在git存储库中获取旧版本文件的副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多提交前的 .tex 文件的版本,我希望得到一份。我有提交的sha1哈希值,它具有我想要的那个文件的版本。我不想替换当前版本的文件。相反,我只想得到一个反映其旧版本状态的单独副本。



很多类似的问题都表明使用 git结帐< sha1> - file.tex ,但这只是继续给错误:pathspec'file.tex'与git已知的任何文件不匹配。



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



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

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

需要 ./ 如果您位于存储库的子目录中,如果您位于存储库的顶层,则可能会被忽略。另外,这在旧版本的git中可能不起作用,在这种情况下,您需要明确提供文件相对于存储库根目录的完整路径。


I have a version of a .tex file 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.

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

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.

Is this possible? How can I do this?

解决方案

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

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存储库中获取旧版本文件的副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆