git grep: Usage, Options & Common CI Errors
git grep searches the contents of tracked files (or any revision) much faster than plain grep over a checkout.
git grep is the right search tool in a repo: it skips ignored files and can search any commit, not just the working tree.
What it does
git grep searches the working tree, the index, or a given revision for a pattern, limited to tracked files, using Git’s fast threaded search.
Common usage
Terminal
git grep "TODO"
git grep -n -i "deprecated" # line numbers, case-insensitive
git grep -l "apiKey" # file names only
git grep "needle" HEAD~5 # search a past revision
git grep -e foo --and -e barOptions
| Flag | What it does |
|---|---|
| -n / --line-number | Show line numbers |
| -l / --files-with-matches | List matching file names |
| -i / --ignore-case | Case-insensitive search |
| -e <pattern> | Specify a pattern (combine with --and/--or) |
| --cached | Search the index instead of the working tree |
Common errors in CI
git grep exits 1 when there are no matches, which CI may treat as a failure - handle it deliberately (e.g. fail the build if a forbidden string IS found: ! git grep -q "console.log"). fatal: not a git repository means it was run outside a checkout.
Related guides
git ls-files: Usage, Options & Common CI Errorsgit ls-files lists files Git knows about in the index. Reference for --others, --modified, --exclude-standard…
git log: Usage, Options & Common CI Errorsgit log shows commit history with flexible formatting. Reference for --oneline, --graph, --pretty, ranges, an…
git show: Usage, Options & Common CI Errorsgit show displays a commit, tag, or object with its diff. Reference for --stat, --name-only, blob viewing, an…
git diff: Usage, Options & Common CI Errorsgit diff shows changes between commits, the index, and the working tree. Reference for --staged, --name-only,…