如何在 git repo 的特定状态下显示文件的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2497051/
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
How can I show the contents of a file at a specific state of a git repo?
提问by richcollins
I want to show the contents of a file given by a path at a specific state of a git repo. I unsuccessfully tried this:
我想在 git repo 的特定状态下显示由路径给出的文件的内容。我没有成功尝试这个:
git show f825334150cd4bc8f46656b2daa8fa1e92f7796d:Katana/source/Git/GitLocalBranch.h
fatal: ambiguous argument
'f825334150cd4bc8f46656b2daa8fa1e92f7796d:Katana/source/Git/GitLocalBranch.h': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
The commit in question didn't modify the file specified. How can I show the contents of a file at a given state (specified by a commit hash) regardless of the involvement of the file in the commit?
有问题的提交没有修改指定的文件。无论文件是否参与提交,如何在给定状态(由提交哈希指定)下显示文件的内容?
回答by stefanB
It's probably problem with your path specification.
您的路径规范可能有问题。
This works, shows version of Makefile in commit b1b22df407417...
这有效,在提交中显示 Makefile 的版本 b1b22df407417...
git show b1b22df407417:Makefile
Or current version in master branch
或主分支中的当前版本
git show master:Makefile
Or current version in exper branch:
或 exper 分支中的当前版本:
git show exper:Makefile
Or previous version on the exper branch:
或 exper 分支上的先前版本:
git show exper^:Makefile
And so on
等等
回答by Mike Seplowitz
The syntax you're using matches that shown in the examples for the git show
manpage, but git
seems to be hinting that you should specify like this:
您使用的语法与git show
联机帮助页示例中显示的匹配,但git
似乎暗示您应该像这样指定:
# I _don't_ think this is your answer...
git show f825334150 -- Katana/source/Git/GitLocalBranch.h
which I've definitely used for git log
and is in its manpage.
我肯定使用git log
过它并且在它的联机帮助页中。
My gut, though,tells me that you using an absolute path and not the path inside the top of your git work tree. You need to make sure that if your .git
directory is at Katana/source/Git/.git
, then you chop off everything before the .git
, like so:
不过,我的直觉告诉我,您使用的是绝对路径,而不是 git 工作树顶部的路径。您需要确保如果您的.git
目录位于Katana/source/Git/.git
,那么您将 之前的所有内容都砍掉.git
,如下所示:
git show f825334150:GitLocalBranch.h
If you're trying to show a git blob from outsidethe git working area, you need to do something like this:
如果您尝试从git 工作区之外显示 git blob ,则需要执行以下操作:
GIT_DIR=Katana/source/Git git show f825334150:GitLocalBranch.h
This will tell git where it can find the data for the your repository.
这将告诉 git 在哪里可以找到存储库的数据。
Bottom line:double-check your paths and make sure they're right. You might need to set a GIT_DIR
if you're not running your command from inside the git work area.
底线:仔细检查您的路径并确保它们是正确的。GIT_DIR
如果您不是从 git 工作区内部运行命令,则可能需要设置 a 。