仅限git grep文件名

Anu*_*ari 8 git grep

我想只查看在文本中有特定单词的不同文件.

current_directory$ git grep 'word'
Run Code Online (Sandbox Code Playgroud)

显示具有匹配单词的文件的每一行

所以我尝试了这个

current_directory$ git grep 'word' -- files-with-matches
current_directory$ git grep 'word' -- name-only
Run Code Online (Sandbox Code Playgroud)

但它没有显示任何输出.

此外,有人可以告诉我如何计算所有文件中'word'的总出现次数.

任何帮助将不胜感激:)

Sun*_*eep 11

错误消息有帮助

$ git grep 'foo' --files-with-matches 
fatal: option '--files-with-matches' must come before non-option arguments

$ git grep --files-with-matches 'foo'
<list of matching files>
Run Code Online (Sandbox Code Playgroud)


为了计算单词,我就是这样做的GNU grep,不确定是否git grep有相关选项

$ grep --exclude-dir=.git -RowF 'foo' | wc -l
717
Run Code Online (Sandbox Code Playgroud)

man grep

-R,--dereference-recursive以递归方式读取每个目录下的所有文件.跟随所有符号链接,与-r不同.

-o, - only-matching仅打印匹配行的匹配(非空)部分,每个此类部分位于单独的输出行上.

-w, - word-regexp仅选择包含构成整个单词的匹配项的行.测试是匹配的子字符串必须位于行的开头,或者前面是非单词构成字符.同样,它必须位于行的末尾或后跟非单词构成字符.单词构成字符是字母,数字和下划线.

-F, - fixed-strings将PATTERN解释为固定字符串(而不是正则表达式)的列表,由换行符分隔,其中任何一个都要匹配.

--exclude-dir = DIR从递归搜索中排除与模式DIR匹配的目录.


小智 6

git grep --files-with-matches 'bar'
Run Code Online (Sandbox Code Playgroud)

“--files-with-matches”需要位于搜索字符串之前