简体   繁体   English

从特定提交中检出特定文件会给出“错误:路径规范文件名不匹配”

[英]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.htmlgit 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我试过了

  • full paths from C:/whatever down to foo.htmlC:/whateverfoo.html完整路径
  • local paths like ./foo.html ./foo.html等本地路径
  • including the enclosing folder, like ../myrepo/foo.html包括封闭文件夹,如../myrepo/foo.html
  • enclosing in single and double quotes用单引号和双引号括起来

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.

相关问题 git 提交错误:pathspec 'commit' 与 git 已知的任何文件都不匹配 - git commit error: pathspec 'commit' did not match any file(s) known to git 错误:pathspec'构建提交'与git已知的任何文件都不匹配 - error: pathspec 'Build commit' did not match any file(s) known to git git 提交错误:pathspec 'xxx' 与 git 已知的任何文件都不匹配 - git commit error: pathspec 'xxx' did not match any file(s) known to git 为什么我收到“提交失败并出现错误:路径规范……与任何文件不匹配”? - Why am I getting “Commit failed with error: pathspec … did not match any file(s)”? 错误:pathspec 'commit' 在使用 commitizen 时与 git 已知的任何文件都不匹配 - error: pathspec 'commit' did not match any file(s) known to git when using commitizen 关于 git 提交错误:pathspec 'A' 与 git 已知的任何文件都不匹配 - About git commit error: pathspec 'A' did not match any file(s) known to git git 提交时出错:pathspec '...' 与 git 已知的任何文件都不匹配 - git error during commit: pathspec '...' did not match any file(s) known to git 签出未被 git 更改的文件时出现 Git 错误:错误:pathspec 'web/.htaccess' 与 ZBA9F11ECC3497D9993B933FDC2BD61EZ5 已知的任何文件都不匹配 - Git error when checking out a file that wasn't changed by git: error: pathspec 'web/.htaccess' did not match any file(s) known to git Xcode - 错误:pathspec '...' 与 git 已知的任何文件都不匹配 - Xcode - error: pathspec '…' did not match any file(s) known to git 错误:pathspec 'CI]"' 与 git 已知的任何文件都不匹配 - error: pathspec 'CI]"' did not match any file(s) known to git
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM