Skip to content

grep Git Command Guide

The git grep command searches for specified patterns in the tracked files in the work tree, blobs registered in the index file, or blobs in given tree objects. It prints lines matching the pattern allowing efficient text searching across repository history.

Terminal window
git grep [-a|--text] [-I] [--textconv] [-i|--ignore-case] [-w|--word-regexp]
[-v|--invert-match] [-h|-H] [--full-name]
[-E|--extended-regexp] [-G|--basic-regexp]
[-P|--perl-regexp]
[-F|--fixed-strings] [-n|--line-number] [--column]
[-l|--files-with-matches] [-L|--files-without-match]
[(-O|--open-files-in-pager) [<pager>]]
[-z|--null]
[-o|--only-matching] [-c|--count] [--all-match] [-q|--quiet]
[--max-depth <depth>] [--[no-]recursive]
[--color[=<when>] | --no-color]
[--break] [--heading] [-p|--show-function]
[-A <post-context>] [-B <pre-context>] [-C <context>]
[-W|--function-context]
[(-m|--max-count) <num>]
[--threads <num>]
[-f <file>] [-e] <pattern>
[--and|--or|--not|(|)|-e <pattern>...]
[--recurse-submodules] [--parent-basename <basename>]
[ [--[no-]exclude-standard] [--cached|--untracked|--no-index] | <tree>...]
[--] [<pathspec>...]
OptionDescription
-a, --textTreat binary files as text
-i, --ignore-caseCase-insensitive search
-w, --word-regexpMatch whole words only
-v, --invert-matchShow non-matching lines
-G, --basic-regexpUse basic regex (default)
-E, --extended-regexpUse extended regex
-P, --perl-regexpUse Perl regex
-F, --fixed-stringsTreat pattern as literal string
-n, --line-numberShow line numbers
-l, --files-with-matchesOnly show filenames
-L, --files-without-matchOnly show filenames without matches
-c, --countShow count of matching lines
-o, --only-matchingShow only matched part
--cachedSearch in index instead of worktree
--no-indexSearch files not under Git control
-p, --show-functionShow function context
ParameterDescription
<pattern>Pattern to search for
<tree>...Tree objects to search in
<pathspec>...Limit search to path patterns
Terminal window
git grep "TODO"

Searches for “TODO” in all tracked files.

Terminal window
git grep -i "error"

Finds “error”, “Error”, “ERROR”, etc.

Terminal window
git grep -w "class"

Matches “class” but not “classes” or “classifier”.

Terminal window
git grep "function" -- "*.js"

Finds “function” only in JavaScript files.

Terminal window
git grep -n "FIXME"

Displays line numbers with matches.

Terminal window
git grep -c "console.log"

Shows how many times each file contains “console.log”.

Terminal window
git grep --cached "password"

Searches staged files instead of working directory.

Terminal window
git grep "import" -- "*.py" "*.java"

Searches Python and Java files only.

Terminal window
git grep -v "DEBUG" -- "*.log"

Shows log lines that don’t contain “DEBUG”.

Terminal window
git grep -L "deprecated" -- "*.cpp"

Lists C++ files that don’t contain “deprecated”.

Terminal window
git grep -l "LICENSE" -- "*.md"

Lists markdown files containing “LICENSE”.

Terminal window
git grep -p "malloc"

Shows function names where “malloc” is found.

Terminal window
git grep "bug" HEAD~10..HEAD

Searches commits in range for “bug”.

Terminal window
git grep --all-match -e "error" -e "warning"

Finds lines containing both “error” and “warning”.

Terminal window
git grep -F "$.ajax"

Treats “$.ajax” as literal text, not regex.

Terminal window
git grep -p func -- "*.c"

Shows function definitions containing “func” in C files.

Search across line boundaries with context

Section titled “Search across line boundaries with context”
Terminal window
git grep -A2 -B1 "struct" -- "*.h"

Shows 1 line before and 2 lines after “struct” matches.

Specify commit ranges like git grep "pattern" HEAD~5..HEAD to search through commits. This examines the blob contents at each specified commit.

What’s the difference between git grep and regular grep?

Section titled “What’s the difference between git grep and regular grep?”

git grep searches Git objects (blobs) in the repository history, while regular grep searches files in the current filesystem. git grep can search index, working tree, or historical versions.

By default, git grep skips binary files but --text treats them as text. Use --textconv to apply git’s text conversion filters for certain binary file types.

git grep searches content by examining blobs, not filenames, so it will find matches across file renames as long as the content exists in repository history.

Why is git grep fast on large repositories?

Section titled “Why is git grep fast on large repositories?”

git grep operates on packed Git objects, avoiding filesystem traversal. It can use multi-threading and works directly with compressed repository data.

Use -e pattern multiple times with --and, --or, --not to combine patterns. Parentheses (, ) create grouping for complex boolean searches.

Can I limit search depth in directory trees?

Section titled “Can I limit search depth in directory trees?”

--max-depth <depth> limits how deep into directory structures git grep searches, useful for large shallow directory structures.

Use --no-index to search files not under Git version control: git grep --no-index "pattern" /path/to/files.

What’s the difference between —cached and —no-index?

Section titled “What’s the difference between —cached and —no-index?”

--cached searches Git index (staged files), --no-index searches files completely outside Git repository (like regular grep).

Can git grep use PCRE (Perl Compatible Regular Expressions)?

Section titled “Can git grep use PCRE (Perl Compatible Regular Expressions)?”

Yes, with -P or --perl-regexp option for advanced regex features like lookbehind/behind, atomic groups, and recursive patterns.

Use --color=always or configure color.grep in git config. Colors highlight matches in terminal output.

Use --recurse-submodules to search in Git submodules in addition to main repository.

--threads <num> enables multi-threaded searching for better performance on large repositories or when searching many files.

By default git grep is case-sensitive. Use -i or --ignore-case for case-insensitive search.

Use -c for counts, -l for filenames, or -L for filenames without matches. For full file content matching, combine with other tools.

How do I search for empty lines or special characters?

Section titled “How do I search for empty lines or special characters?”

Use patterns like ^$ for empty lines, \t for tabs, or \\[0-9]+ for numbers. Escape special regex characters appropriately.

What’s the difference between —heading and —break?

Section titled “What’s the difference between —heading and —break?”

--heading groups matches by file with filename headers, --break adds extra blank lines between file groups for better visual separation.

  1. Code Analysis: Locate function definitions, variable usages, or API calls across codebase
  2. Bug Hunting: Search for specific error messages or problematic code patterns
  3. Code Reviews: Find where certain patterns or practices are used before changes
  4. Refactoring: Identify all locations using deprecated functions or variables
  5. Security Audits: Search for hardcoded passwords, API keys, or sensitive data
  6. Consistency Checks: Ensure coding standards compliance across large repositories