Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在git grep--pathspec中引号很重要?_Git - Fatal编程技术网

为什么在git grep--pathspec中引号很重要?

为什么在git grep--pathspec中引号很重要?,git,Git,使用文件: .\contains-x.txt .\subdir\also-contains-x.txt 如果引用pathspec,我会得到不同的结果: $ git grep x -- *.txt contains-x.txt:x $ git grep x -- '*.txt' contains-x.txt:x subdir\also-contains-x.txt:x 为什么会有差异?如果没有引号,shell会扩展*,因此,在git获得它之前,您不会将*.txt传递给git grep,而是传

使用文件:

.\contains-x.txt
.\subdir\also-contains-x.txt
如果引用pathspec,我会得到不同的结果:

$ git grep x -- *.txt
contains-x.txt:x

$ git grep x -- '*.txt'
contains-x.txt:x
subdir\also-contains-x.txt:x

为什么会有差异?

如果没有引号,shell会扩展
*
,因此,在
git
获得它之前,您不会将
*.txt
传递给
git grep
,而是传递当前目录中与此模式匹配的文件列表。

如果没有引号,您的
patspec
将由shell扩展。这与下面的
echo
示例类似

$ touch contains-x.txt
$ ls
contains-x.txt
$ echo *.txt
contains-x.txt
$ echo '*.txt'
*.txt

答案很简单

将“”添加到git命令时,它只需在所有项目(包括子文件夹)上执行它, 比如说

git add *.txt   -> will add all the *.txt files in the current directory
git add '*.txt' -> will add all the .txt file in any directory (recursive)

你刚才重申了这个问题,但没有解释原因。已经有两个答案解释了这是因为在将glob传递给git之前,shell在没有引号的情况下将glob扩展到当前目录中的文件。