Skip to content
Latchkey

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 bar

Options

FlagWhat it does
-n / --line-numberShow line numbers
-l / --files-with-matchesList matching file names
-i / --ignore-caseCase-insensitive search
-e <pattern>Specify a pattern (combine with --and/--or)
--cachedSearch 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

Run this faster and cheaper on Latchkey managed runners. Start free →