Skip to content

git grep — DevOps Quick Reference ​

Core commands ​

TaskCommand
Search working tree (tracked files)git grep <pattern>
Search a revision (any tag/branch/commit)git grep <pattern> <rev>
Search staged content onlygit grep --cached <pattern>
Search including untracked filesgit grep --untracked <pattern>
Search revision range (A..B)git grep <pattern> <revA>..<revB>
Fixed-string (not regex)git grep -F <string>
Case-insensitivegit grep -i <pattern>
Invert matchgit grep -v <pattern>

Output control ​

FlagWhat it returns
-nLine numbers
-cColumn offset (1-indexed)
-lFile names only (CI gate)
-LFiles NOT matching
--countMatch count per file
-oMatched text only
-hSuppress filename
-qQuiet, exit code only (CI)
-pFunction/class name enclosing match
-WEntire function/class body with match
--breakBlank line between files
--headingFile name as section heading
--threads NParallel execution

Boolean expressions ​

git grep --and -- -e <p1> -e <p2>           # lines matching both
git grep --or  -- -e <p1> -e <p2>           # lines matching either
git grep --not -- -e <p1>                    # lines NOT matching
git grep --all-match -- -e <p1> -e <p2>     # files matching ALL

CI gate one-liners ​

bash
# Fail if staged files contain TODO
git grep --cached -q "TODO" && exit 1

# Fail if any checked-in Python file has an API key pattern
git grep -ql "AKIA[0-9A-Z]\{16\}" HEAD -- '*.py' && exit 1

# Corpus count for CI matrix decision
git grep --count "func main" HEAD -- '*.go'

# Find all files with a pattern at a tag (no checkout)
git grep -l "secret" v2.3.0

# Boolean: files with both "password" and a URL
git grep --all-match -l -e "password" -e "https\?://" HEAD -- '*.json'

# Function-level context for review
git grep -pn "deprecated_fn" HEAD -- '*.py'

# Line + column for CI file annotations
git grep -cn "TODO\|FIXME\|HACK" origin/main -- '*.rs'

Config ​

bash
# Default threads (auto: CPU/2)
git config grep.threads 4

# Default to extended regex (grep -E equivalent)
git config grep.extendedRegexp true

# Default to line numbers
git config grep.lineNumber true

# Always show column numbers
git config grep.column true

# Skip binary files by default
git config grep.binary false

Exit codes ​

CodeMeaning
0Match found
1No match found
2Error (invalid pattern, broken revision)

Use -q + exit code for CI gates: git grep -q <pattern> && echo "found" || echo "clean"

Pitfalls to avoid ​

  • git grep "pattern" -- *.py — shell expands the glob; use -- '*.py'
  • Patterns are regex by default — use -F for literal strings
  • Revision range A..B searches paths at both revs, not the diff between them

Git Master — Git Reference for Every Skill Level