git grep by file extensions

GitGrepFile Extension

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

Yes, for example:

git grep res -- '*.js'

Solution 2 - Git

Try doing this :

find . -type f -iname '*.js' -exec grep -i 'pattern' {} +

Solution 3 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVinayView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - GitGilles QuenotView Answer on Stackoverflow
Solution 3 - GitKay VView Answer on Stackoverflow