Faster than grep on a repo. Plays well with revspecs.
Search for a pattern in tracked content. Faster than POSIX grep because it walks the index, not the filesystem; and it can search any historical revision, not just the current working tree.
Grep takes a pattern and (optionally) a tree-ish or pathspec. Without a tree-ish, it searches the working tree for tracked files. With a tree-ish, it greps inside that tree's blobs.
git grep [<options>] <pattern> [<tree-ish>...] [[--] <pathspec>...]| Flag | What it does |
|---|---|
-n | Show line numbers. |
-l | List only matching files. |
-c | Count matches per file. |
-i | Case-insensitive. |
-E | Extended regex. |
-P | PCRE. |
--cached | Search the index, not the working tree. |
<tree-ish> | Search inside that revision. |
grep -r.$ git grep -n 'def parse_request'$ git grep -n 'session_token' v1.2$ git grep -c 'TODO'$ git grep 'malloc' -- '*.c'git grep over grep -r in any Git repo. It honors .gitignore, ignores .git/, and is faster.-P for advanced regex when you need it.--untracked if you want them included.-P) requires Git built with PCRE support — most distros do, some don't.Hit each option, then Check answers. Score is recorded; Next is always open.