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

1k Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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'