git - 仅使用 Git grep 的文件名

标签 git grep

我只想查看文本中包含特定单词的不同文件。

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/

相关文章:

bash - 如何使用AWK按某列抓取文件中的一行

git - 告诉 git 可以删除未跟踪的文件

windows - NodeJs - 在 Windows 上访问 Git 索引

git - 从项目中的项目移动包含子项目/保留的目录

git - 我可以使用 meld 以交互方式查看 git add --patch 的帅哥吗

ubuntu - ack(ack-grep) 忽略嵌套目录

git - 排队时如何使用自托管代理清理构建

linux - 将 tmpfs 视为 grep 的设备

linux - Grep Linux 文件系统中的常规文件并显示其内容

regex - Grep 获取某个范围内的数字 - 检查 WAV 质量