“git checkout -- *”返回“错误:路径规范与git已知的任何文件都不匹配”

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

"git checkout -- *" returns "error: pathspec did not match any files known to git"

git

提问by pepuch

As in question, if I want to discard changes in working directory I run command git checkout -- *but git returns information error: pathspec 'databaseName.tmp' did not match any file(s) known to git.. I'm not git master and I don't know how to solve it. Any idea?

正如所讨论的,如果我想放弃工作目录中的更改,我运行命令git checkout -- *但 git 返回信息error: pathspec 'databaseName.tmp' did not match any file(s) known to git.。我不是 git master,我不知道如何解决它。任何的想法?

回答by torek

As genisagenoted in a comment, you're asking git explicitly to check out the file databaseName.tmp, as if you'd typed in:

正如genisage在评论中指出的那样,您明确要求 git 检查文件databaseName.tmp,就好像您输入了:

git checkout -- databaseName.tmp

This is because the *you typed in is handled by the shell, before git ever has a chance to see your command. The shell replaces *with all the file names in your current working directory,1and then having done that, thenruns git, without any indication that the actual command you entered had *in it, rather than all those names.

这是因为*在 git 有机会看到您的命令之前,您输入的内容已由 shell 处理。shell 将替换*为您当前工作目录中的所有文件名1,然后执行该操作,然后运行git,而没有任何迹象表明您输入的实际命令包含*其中,而不是所有这些名称。

Again, git has no idea that you used the asterisk *character, all it sees is a list of file names, includingany top-level ignored files that are not being stored in git.

同样,git 不知道您使用了星号*字符,它看到的只是文件名列表,包括没有存储在 git 中的任何顶级忽略文件。

Confusingly, if you manage somehow2to pass a literal asterisk *to git, gitwill expand the *, but with a different set of file names:those known to git. That would do what you want.

令人困惑的是,如果您设法以某种方式2将文字星号传递*给 git,git将扩展*, 但使用一组不同的文件名:git 已知的文件名。那会做你想做的。

There's an easier way, though: git will check out directories recursively by checking out all the files it knows about inthat directory. So, instead of using *, simply use .to ask git to check out the current directory:

还有一个更简单的方法,但:git会通过检查所有它知道的文件递归地检查目录该目录中。因此,不要使用*,只需使用.git 来检查当前目录:

git checkout -- .

If git knows about ./aand ./bbut not ./databaseName.tmp, this will check out ./aand ./band not attempt to do anything with ./databaseName.tmp.3

如果git的知道./a./b,但不./databaseName.tmp,这将检查./a./b,而不是试图做任何事情./databaseName.tmp3



1More accurately, files whose names do not start with a leading dot ..

1更准确地说,名称不以前导点开头的文件.

2And in fact, it's quite easy to manage, e.g., simply insert a backslash \in front of the asterisk: git checkout -- \*. Or, use single or double quotes, both of which protect against shell globbing. Single quotes also inhibit shell variable expansion, while double quotes permit variable expansion but inhibit globbing.

2事实上,它很容易管理,例如,只需\在星号前插入一个反斜杠:git checkout -- \*。或者,使用单引号或双引号,这两者都可以防止 shell globbing。单引号还禁止 shell 变量扩展,而双引号允许变量扩展但禁止通配符。

3It's worth pointing out a subtle difference here: *expands to file names that do not start with ., while asking git to check out .causes it to check out all files in the directory, includingthose whose names start with .. Hence both git checkout -- \*and git checkout -- *will notundo changes to a file named .secret, for instance, while git checkout -- .willundo such changes.

3这是值得在这里指出一个微妙的差异:*扩展为不开始的文件名.,而要求的git检查出.原因是检查所有目录中的文件,包括那些名字开始.。因此,git checkout -- \*git checkout -- *不会撤消对名为 的文件的更改.secret,例如, whilegit checkout -- .撤消此类更改。