git grep by file extensions
Asked Answered
V

4

131

I know that, if I wanted to grep for a pattern only on files with certain extensions, I could do this:

// searches recursively and matches case insensitively in only javascript files
// for "res" from the current directory
grep -iIr --include=*.js res ./

I've been trying to search for a way to do this via git grep as well (to take advantage of git grep's speed from the indexes it stores with its tree), but to no avail. I saw here that excluding certain file types is not possible.

Versicle answered 13/12, 2012 at 20:13 Comment(0)
G
206

Yes, for example:

git grep res -- '*.js'
Gnosticism answered 13/12, 2012 at 21:16 Comment(11)
One slight modification - if you're not in the root of your repository, git grep res -- '/*.js' might be better...Laky
@twalberg: But the comment suggests that he wants to look from the current directory [down], surely?Gnosticism
Ah... right... The question didn't specifically say that, but the comment in the code block does.Laky
I do wish to search from the current directory down.Versicle
Just saw your comment @twalberg. That's exactly what I needed for searching from a given path. :-)Versicle
@Vinay: The simplest way is to cd to where you wan to search from and run git grep from there. This is the common use case that git grep was designed for.Gnosticism
Just extra information: If you wish to specify a set of file extensions you can use git grep res -- *.js *.cs this is covered in another questionUlulate
what does the -- accomplish?Siphon
An important thing to not forget is the single quotes around '*.js'. If they are not there, it will not work. (the shell will intercept it before it reaches git).Earthworm
git grep res -- '*.proto' gave me what I needed. Thanks.Surat
aehlke -- is a common delimiter in many of git's commands to separate arguments with a set of files to operate on.Miniver
C
1

Try doing this :

find . -type f -iname '*.js' -exec grep -i 'pattern' {} +
Chanty answered 13/12, 2012 at 20:14 Comment(2)
{} is the current file and + means to treat the maximum arguments at the same time unlike {} \; (only one file at a time). Check man findAbsher
The question asks for "git" grep.Candelaria
T
0

if you want to search across all branches, you can use the following:

git log -Sres --all --name-only -- '*.js'

(I see you specify git grep; to me the approach here seems simpler and easier to remember--more like other operations I need commonly.)

Tobytobye answered 26/6, 2020 at 23:29 Comment(0)
C
0

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".

Coercive answered 30/1 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.