Quick summary
# Search only in files ending in .h or .c
git grep 'my search' -- '*.[ch]'
Details
man git grep
shows the following. Check out the description of <pathspec>
, as well as the several examples here:
<pathspec>...
If given, limit the search to paths matching at least one pattern.
Both leading paths match and glob(7) patterns are supported.
For more details about the <pathspec> syntax, see the pathspec
entry in gitglossary(7).
EXAMPLES
git grep 'time_t' -- '*.[ch]'
Looks for time_t in all tracked .c and .h files in the working
directory and its subdirectories.
git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)
Looks for a line that has #define and either MAX_PATH or PATH_MAX.
git grep --all-match -e NODE -e Unexpected
Looks for a line that has NODE or Unexpected in files that have
lines that match both.
git grep solution -- :^Documentation
Looks for solution, excluding files in Documentation.
Two really good examples above are:
# Looks for time_t in all tracked .c and .h files in the working
# directory and its subdirectories.
git grep 'time_t' -- '*.[ch]'
# Looks for solution, excluding files in Documentation.
git grep solution -- :^Documentation
Notice the glob *.[ch]
pattern in the first one to mean "anything.h or anything.c", and the :^
in the second one to mean "not". So, apparently :^Documentation
means "not in the Documentation
file or folder".
git grep res -- '/*.js'
might be better... – Laky