Search code examples
gitgit-grep

"git grep <pattern>" for just python (or any type of) files?


When I'm trying to find a pattern in a git-based project, I type:

git grep <pattern>

and git searches the entire project for that pattern. Sometimes this finds matches in non-source files, which makes finding what I'm looking for harder.

How can I tell it to only search Python (.py) or some other source files?


Solution

  • You can use :

    Sytntax

    git grep <pattern> -- '*.<ext>'
    

    Where :

    • pattern is the pattern to look for
    • ext is the extension,in our case it is to look in all the files that contain <ext>

    Example

    git grep HelloWorld -- '*.json'