How to git grep only a set of file extensions

前端 未结 2 1544
孤独总比滥情好
孤独总比滥情好 2021-02-13 02:06

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

         


        
相关标签:
2条回答
  • 2021-02-13 02:58

    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.

    0 讨论(0)
  • 2021-02-13 03:05

    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.

    0 讨论(0)
提交回复
热议问题