" git checkout-*"返回“错误:pathspec与git已知的任何文件都不匹配"; [英] "git checkout -- *" returns "error: pathspec did not match any files known to git"
问题描述
有问题的是,如果我想放弃工作目录中的更改,请运行命令git checkout -- *
,但是git返回信息error: pathspec 'databaseName.tmp' did not match any file(s) known to git.
.我不是git master,也不知道如何解决.有什么主意吗?
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?
推荐答案
为 genisage
As genisage noted in a comment, you're asking git explicitly to check out the file databaseName.tmp
, as if you'd typed in:
git checkout -- databaseName.tmp
这是因为在git有机会看到您的命令之前,您输入的*
是由外壳程序处理的. Shell将*
替换为当前工作目录中的所有文件名 1 ,然后执行 then 运行git
,而没有任何指示输入的命令中带有*
,而不是所有这些名称.
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,1 and then having done that, then runs git
, without any indication that the actual command you entered had *
in it, rather than all those names.
同样, git不知道您使用星号*
字符,它看到的只是文件名列表,包括任何顶级被忽略的文件未存储在git 中.
Again, git has no idea that you used the asterisk *
character, all it sees is a list of file names, including any top-level ignored files that are not being stored in git.
令人困惑的是,如果您设法以某种方式 2 将文字星号*
传递给git,则 git 会扩展*
,但是具有不同的含义一组文件名: git已知的那些文件名.那会做你想要的.
Confusingly, if you manage somehow2 to pass a literal asterisk *
to git, git will expand the *
, but with a different set of file names: those known to git. That would do what you want.
不过,有一种更简单的方法:git将通过检出该目录中已知的所有文件来递归检出目录.因此,代替使用*
,只需使用.
要求git签出当前目录:
There's an easier way, though: git will check out directories recursively by checking out all the files it knows about in that directory. So, instead of using *
, simply use .
to ask git to check out the current directory:
git checkout -- .
如果git知道./a
和./b
但不了解./databaseName.tmp
,则它将检出./a
和./b
,并且不会尝试对./databaseName.tmp
进行任何操作. 3
If git knows about ./a
and ./b
but not ./databaseName.tmp
, this will check out ./a
and ./b
and not attempt to do anything with ./databaseName.tmp
.3
1 更准确地说,名称不是以前导点.
开头的文件.
1More accurately, files whose names do not start with a leading dot .
.
2 实际上,它非常易于管理,例如,只需在星号前面插入反斜杠\
:git checkout -- \*
.或者,使用单引号或双引号,这两个引号都可以防止shell globbing.单引号还可以抑制shell变量的扩展,而双引号则可以允许变量的扩展但可以防止globbing.
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.
3 此处需要指出一个细微的区别:*
扩展为不以.
开头的文件名,同时要求git签出.
导致其签出目录中的所有文件,包括,其名称以.
开头的文件.因此,例如git checkout -- \*
和git checkout -- *
都将 not 撤消对名为.secret
的文件的更改,而git checkout -- .
将撤消此类更改.
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, including those whose names start with .
. Hence both git checkout -- \*
and git checkout -- *
will not undo changes to a file named .secret
, for instance, while git checkout -- .
will undo such changes.
这篇关于" git checkout-*"返回“错误:pathspec与git已知的任何文件都不匹配";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!