- Developers who already know basic commit and branch actions
- Readers who want to understand command boundaries and risk
Command Reference
git-grep Tutorial
Explains how to use git-grep to search text inside the repository.
- A basic mental model of worktree, index, and commits
- Comfort reading `git status` and a small commit graph
- Using local cleanup commands on already shared history
- Continuing to rewrite before confirming a recovery path
The short version
git-grep is used to search text inside the repository.
When it is a good fit
- when you need to search text inside the repository
- when you want this step to be repeatable instead of ad hoc
- when you need a clearer mental model of what Git is recording or updating
Basic example
git grep "TODO"
What to watch most closely
Learn the default behavior first. Many surprises come from adding flags before the base behavior is clear.
A safer working habit
Pair it with status, log, or diff so you can confirm what actually changed.
Useful angles for understanding it
- Understand the default behavior clearly
- Use it in day-to-day Git routines
- Reuse it safely in scripts or team habits
Related reading
Read it alongside git status, git log, and git show so it is easier to see how the command changes history, refs, the index, or the working tree.
What problem this command solves in a workflow
git grep searches for matching text patterns across tracked files in the repository, ignoring untracked files and .gitignore content by default. Think of it in the context of "how do I quickly locate specific content in the codebase?"
Typical use cases
- Quickly find all references to a function, variable, or constant across the codebase.
- Combine with
--cachedto search staged content, or specify a particular commit/branch to search historical versions. - Customize search results with options like
-n(line numbers),-i(case-insensitive), and-c(count).
Diagram view
Search patternTarget scope (working tree / staged / specific commit)
Matching lines with file locations
git grep only searches tracked files, so results are not polluted by build artifacts or temporary files.
Special cases and boundaries
git greponly searches tracked files by default; files in.gitignoreare excluded from the search.- You can combine multiple search patterns with
--and,--or, and--notfor more precise matching. - The search scope can be limited by specifying a commit, tree object, or path, such as
git grep "TODO" v1.0 -- src/. - In large repositories, git grep is typically faster than system grep because Git only traverses indexed files rather than scanning the entire filesystem.