[英]Checking out a specific file from a specific commit gives "error: pathspec filename did not match"
I'm using git on Windows.我在 Windows 上使用 git。
Say I want to check out, not commit fe4fd (abbreviated to make examples shorter) but specifically file foo.html from commit fe4fd.假设我想签出,而不是提交 fe4fd(缩写为使示例更短),而是特别提交 fe4fd 中的文件 foo.html。
According to the documentation I should be able to use根据我应该能够使用的文档
git checkout fe4fd foo.html
and that should work.这应该有效。 Or possibly或者可能
git checkout fe4fd -- foo.html
because the dashes seem to be optional.因为破折号似乎是可选的。
Anyway.反正。 I'm at the right directory.我在正确的目录中。
If I do a git status foo.html
or git reflog foo.html
etc. everything seems normal and it knows the file is there.如果我执行git status foo.html
或git reflog foo.html
等,一切似乎都很正常,它知道文件在那里。
everything looks normal一切看起来都很正常
I can do this:我可以做这个:
git checkout fe4fd
and it works fine它工作正常
I can do this:我可以做这个:
git checkout foo.html
but I can't do但我做不到
git checkout fe4fd foo.html
because every time I get因为每次我得到
error: pathspec 'foo.html' did not match any file(s) known to git.错误:pathspec 'foo.html' 与 git 已知的任何文件都不匹配。
I've tried我试过了
C:/whatever
down to foo.html
从C:/whatever
到foo.html
完整路径./foo.html
./foo.html
等本地路径../myrepo/foo.html
包括封闭文件夹,如../myrepo/foo.html
what am I missing?我错过了什么?
TIA. TIA。
UPDATE : https://stackoverflow.com/users/6330106/elpiekay got me to the answer.更新: https : //stackoverflow.com/users/6330106/elpiekay让我找到了答案。 ElpieKay if you post your comment as an answer I will mark it as correct. ElpieKay 如果您发表评论作为答案,我会将其标记为正确。
The answer is, at the time of that commit , the file wasn't at that path , because a parent folder had been renamed after that commit.答案是,在提交时,文件不在那个路径上,因为在提交后父文件夹已被重命名。
I figured this out by reading the documentation for git log
and learning that it wouldn't follow a file's history through a path change unless you include the --follow
flag.我通过阅读git log
的文档并了解到除非您包含--follow
标志,否则它不会通过路径更改来跟踪文件的历史记录。
I did what ElpieKay suggested, and the file wasn't apparently part of the commit.我按照 ElpieKay 的建议做了,该文件显然不是提交的一部分。 Which was wrong, because I knew it was.这是错误的,因为我知道它是。 But the file I was trying to check out was, say bar/foo.html
and that path didn't exist at the time of the checkout I was looking for.但是我试图签出的文件是bar/foo.html
并且在我正在寻找的签出时该路径不存在。 At the time of that checkout it was at baz/foo.html
.在结帐时,它位于baz/foo.html
。
To prove this, I created a new baz/
folder, cd
'd to it, and checked out the file from the commit.为了证明这一点,我创建了一个新的baz/
文件夹, cd
到它,并从提交中检出文件。 This worked.这奏效了。
When you run git checkout [commit] -- [paths]
, then the paths have to exist in that commit (to be more precise: in the tree referenced by the commit).当您运行git checkout [commit] -- [paths]
,路径必须存在于该提交中(更准确地说:在提交引用的树中)。 If the file has been renamed since, you have to find the location of the file back then and use that.如果该文件已被重命名,则您必须在当时找到该文件的位置并使用该位置。
Another possibility to update your working tree version of the file would be to use git show
:更新文件的工作树版本的另一种可能性是使用git show
:
git show commit:old/path/to/file.ext > new/path/to/file.ext
You can use commit:./path/to/file
if you require paths relative to the current directory.如果您需要相对于当前目录的路径,您可以使用commit:./path/to/file
。 Otherwise, the paths are anchored to your repository root.否则,路径将锚定到您的存储库根目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.