git grep by file extensions
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
git grep can search only the file types you care about by combining the search pattern with pathspec filters after --. This is usually the fastest way to search within tracked files of specific extensions inside a Git repository. The important part is the syntax: the pattern comes first, -- separates it from pathspecs, and the pathspecs limit which files are searched.
Search One Extension
To search only JavaScript files:
This means:
- search for the text
initialize - only consider tracked files matching
*.js
The -- separator matters because it tells Git that everything after it is a path filter, not another option or part of the pattern.
Search Multiple Extensions
You can pass more than one pathspec:
This searches Python and shell files only.
That is often more convenient than piping through grep or find because Git already knows which files are tracked and can search them efficiently.
Restrict by Directory and Extension
You can combine directory scope with file extension filters:
Or:
This is useful when the repository contains many languages and you want to narrow the search surface quickly.
Excluding Extensions
Sometimes you want to search broadly but skip certain file types.
Pathspec exclusion rules are powerful, but they can be harder to read. If the intent is simply “search only these two file types,” positive inclusion patterns are usually clearer.
Search a Specific Commit or Branch
git grep can also search a revision rather than just the working tree.
Or:
This is useful for historical investigation without checking out another branch manually.
Search Line Numbers and Ignore Case
Useful flags combine naturally with extension filters:
This gives:
- '
-nline numbers' - '
-icase-insensitive search'
You can also use extended regex with -E if needed.
Why git grep Is Often Better Than Plain grep
Compared with generic filesystem search, git grep has a few practical advantages in repositories:
- it respects Git’s view of tracked content
- it is often faster on large repos
- it works naturally with revisions and branches
- it avoids searching irrelevant generated files if they are not tracked
That makes it especially useful in code review, refactoring, and incident response workflows.
Quoting Matters
Patterns like *.js should usually be quoted so the shell does not expand them before Git sees them.
Good:
Riskier:
If the shell expands the wildcard in the current directory first, the command may behave differently from what you intended.
Practical Examples
Find all references to a config key in YAML only:
Search only TypeScript source, not tests:
Search Markdown docs for a term:
These combinations are where git grep really earns its keep.
Common Pitfalls
The biggest mistake is forgetting the -- separator and then wondering why the file filter is ignored or misparsed. Another is failing to quote the wildcard pathspec, which lets the shell expand it too early. Developers also often overcomplicate extension filtering with external tools when git grep already supports pathspecs directly. Finally, if a file is untracked, git grep will not search it unless you choose a different tool for that job.
Summary
- Use
git grep "pattern" -- '*.ext'to search by file extension. - Pass multiple pathspecs to search several extensions at once.
- Quote wildcard patterns so Git, not the shell, interprets them.
- Combine extension filters with flags like
-n,-i, and revision arguments. - Prefer
git grepover generic filesystem search when you want tracked-repo-aware results.
Related reading
- git hooks is there a clone hook?
- Git, How do I list only local branches?
- Git How do I list only local branches?
- git how to disable push
- git how to move some commits to new branch
- Git How to rebase to a specific commit?
- git how to rename a branch both local and remote?
- Git, How to reset origin/master to a commit?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.