我只想查看文本中包含特定单词的不同文件。
current_directory$ git grep 'word'
显示文件中具有匹配词的每一行。
所以我试了一下
current_directory$ git grep 'word' -- files-with-matches
current_directory$ git grep 'word' -- name-only
但它没有显示任何输出。
另外,如何计算所有文件中'word'的总出现次数?
最佳答案
错误信息有助于:
$ 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>
为了计算字数,这就是我用 GNU grep 做的方式(我不确定 git grep
是否有相关选项):
$ grep --exclude-dir=.git -RowF 'foo' | wc -l
717
来自 man grep
:
-R, --dereference-recursive
Read all files under each directory, recursively. Follow all symbolic links, unlike -r.-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the underscore.-F, --fixed-strings
Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.--exclude-dir=DIR
Exclude directories matching the pattern DIR from recursive searches.
关于git - 仅使用 Git grep 的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48970788/