Appearance
git grep — DevOps Quick Reference
Core commands
| Task | Command |
|---|---|
| Search working tree (tracked files) | git grep <pattern> |
| Search a revision (any tag/branch/commit) | git grep <pattern> <rev> |
| Search staged content only | git grep --cached <pattern> |
| Search including untracked files | git grep --untracked <pattern> |
| Search revision range (A..B) | git grep <pattern> <revA>..<revB> |
| Fixed-string (not regex) | git grep -F <string> |
| Case-insensitive | git grep -i <pattern> |
| Invert match | git grep -v <pattern> |
Output control
| Flag | What it returns |
|---|---|
-n | Line numbers |
-c | Column offset (1-indexed) |
-l | File names only (CI gate) |
-L | Files NOT matching |
--count | Match count per file |
-o | Matched text only |
-h | Suppress filename |
-q | Quiet, exit code only (CI) |
-p | Function/class name enclosing match |
-W | Entire function/class body with match |
--break | Blank line between files |
--heading | File name as section heading |
--threads N | Parallel 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 ALLCI 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 falseExit codes
| Code | Meaning |
|---|---|
| 0 | Match found |
| 1 | No match found |
| 2 | Error (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
-Ffor literal strings - Revision range
A..Bsearches paths at both revs, not the diff between them