LinuxCommandLibrary

git-grep

TLDR

Search for pattern

$ git grep "[pattern]"
copy
Search with line numbers
$ git grep -n "[pattern]"
copy
Case insensitive search
$ git grep -i "[pattern]"
copy
Search in specific commit
$ git grep "[pattern]" [commit]
copy
Show only filenames
$ git grep -l "[pattern]"
copy

SYNOPSIS

git grep [options] pattern [revision] [--] [path]

DESCRIPTION

git grep searches tracked files for patterns. It's optimized for git repositories, searching only tracked files and supporting search across history.
Unlike regular grep, git grep ignores untracked files and can search any commit. It's significantly faster on large repos because it uses git's index.
git grep provides fast, git-aware text search.

PARAMETERS

PATTERN

Search pattern (regex).
REVISION
Commit/branch to search.
-n, --line-number
Show line numbers.
-i, --ignore-case
Case insensitive.
-l, --files-with-matches
Show only filenames.
-c, --count
Show match counts.
-w, --word-regexp
Match whole words.
-e PATTERN
Pattern argument.
--help
Display help information.

CAVEATS

Only searches tracked files. Regex syntax differs from grep. Binary files skipped by default.

HISTORY

git grep is a core Git command providing optimized search that understands git's object model and history.

SEE ALSO

grep(1), git-log(1), rg(1)

Copied to clipboard