Git grep只打印匹配的图案

时间:2019-08-02 17:28:54

标签: git grep

grep的手册中,我们有-o打印--only-matching模式。说echo foobar | grep foo将返回foobar,但是将-o添加到grep只会得到foo

许多grep选项(例如-P-c等)可以与git结合使用,以搜索Git索引中的所有文件。但是,git grep -o PAT会触发error: unknown switch o'`。

如何为Git索引中的每个文件仅打印匹配的字符串?即“ git grep -o PAT

我的审判:

for f in `git ls-files`; do grep -o PAT $f; done

1 个答案:

答案 0 :(得分:4)

git grep 2.18没有选项-o|--only-matchinggit grep 2.19没有。如果您的版本低于2.19,则不能使用该选项。要使用它,您必须升级。