How to git grep only a set of file extensions

天大地大妈咪最大 提交于 2019-12-03 10:48:31

Use:

git grep "MyFunc" -- '*.cpp' '*.h'

The quotes are necessary so that git expands the wildcards rather than the shell. If you omit them, it'll only search files in the current directory, rather than including subdirectories.

This can be achieved when using git bash in Windows by using:

git grep "MyFunc" -- *.{cpp,h}

or even simpler:

git grep "MyFunc" -- *.cpp *.h

The explanation of pathspec on the git glossary mentions that patterns are matched using fnmatch(3). Which matches patterns (including multiple characters) as described by Shell and Utilities volume of IEEE Std 1003.1-2001, Section 2.13.1 and leads to basic regular expressions matching multiple characters and gave me the first solution.

Further research lead me to the second solution by looking into documentation on glob.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!