Command Reference

git-grep Tutorial

Explains how to use git-grep to search text inside the repository.

Who This Is For
  • Developers who already know basic commit and branch actions
  • Readers who want to understand command boundaries and risk
Prerequisites
  • A basic mental model of worktree, index, and commits
  • Comfort reading `git status` and a small commit graph
Common Risks
  • 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 --cached to 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

In-repository text searchThe grep command performs text pattern matching against tracked files, faster and cleaner than a generic system grep.
Input
Search patternTarget scope (working tree / staged / specific commit)
Output
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 grep only searches tracked files by default; files in .gitignore are excluded from the search.
  • You can combine multiple search patterns with --and, --or, and --not for 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.