Search Tracked Files With git grep

Grep working tree or historical revisions without scanning .git internals.

macOSsearchgitcliLow risk
Run git grep -n "pattern" to search tracked files with line numbers.

01Command

Command
$ git grep -n "TODO" HEAD~5

02What it does

git grep searches only tracked content, respects .gitignore and can target any commit or tag, so you can compare what a string looked like at different revisions. It is faster than recursive grep over a large checkout.

03Risk

RiskLow risk
RequirementsGit 2.20+ (install with Homebrew or Xcode Command Line Tools)
CompatibilitymacOS with Git installed (Homebrew or Xcode Command Line Tools)
!Warning: Passing an explicit tree like HEAD~5 searches that revision; omitting it searches the working tree.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • git-grep documentation
    Git · vendor-doc · 2026-09-20
    Evidence: Documents git grep, which searches tracked files at a given tree.

Last verified: 2026-10-13 · Category: Developer

06Related solutions