Progress:
TIER 1 · MODULE 29· Basics

git grep

Faster than grep on a repo. Plays well with revspecs.

🎯 What & why

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.

🧠 Mental model

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.

🛠️ Synopsis

git grep [<options>] <pattern> [<tree-ish>...] [[--] <pathspec>...]

🎚️ Switches & options

FlagWhat it does
-nShow line numbers.
-lList only matching files.
-cCount matches per file.
-iCase-insensitive.
-EExtended regex.
-PPCRE.
--cachedSearch the index, not the working tree.
<tree-ish>Search inside that revision.

💡 Use cases

🧪 Examples

Find a function definition in the working tree.
$ git grep -n 'def parse_request'
Find every place a variable was used in v1.2.
$ git grep -n 'session_token' v1.2
Count matches across the codebase.
$ git grep -c 'TODO'
Limit by path.
$ git grep 'malloc' -- '*.c'

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.