Like git grep only a set of file extensions

How do you do git grep and limit files checked for a set of files. I would like to be able to grep the contents of .cpp and .h files looking for MyFunc. eg:

git grep "MyFunc" -- *.[hc]* 

However, this also corresponds to .c and .cs files.

+8
git grep file-extension
Feb 06 '14 at 9:59
source share
2 answers

This can be achieved by using git bash on Windows with:

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

or even simpler:

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

The explanation of pathspec in the git glossary mentions that patterns are matched using fnmatch (3) . Which corresponds to patterns (including several characters), as described in Shell and the IEEE Std 1003.1-2001 utility, section 2.13.1, and leads to basic regular expressions corresponding to several characters , and gave me the first solution.

Further research led me to a second solution by looking at the glob documentation.

+4
Feb 06 '14 at 9:59
source share

Using:

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

Quotation marks are required so that git extends wildcards, not the shell. If you omit them, it will only search for files in the current directory, and not include subdirectories.

+8
Oct 17 '15 at 12:36
source share



All Articles