LinuxCommandLibrary

git-grep

Search patterns in tracked files

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, optimized specifically for Git repositories. Unlike regular grep, it ignores untracked files and can search any commit in the repository history.
The command is significantly faster on large repositories because it uses Git's index rather than scanning the filesystem directly. It supports the same regex syntax as grep and integrates seamlessly with Git's revision and path specifications.

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)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community