LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

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.
-v, --invert-match
Select non-matching lines.
-A n, -B n, -C n
Show n lines of context after, before, or around each match.
-E, --extended-regexp
Use extended (ERE) regex instead of the default basic (BRE) regex.
-F, --fixed-strings
Treat pattern as literal strings, not regex.
--cached
Search the index instead of the working tree.
--untracked
Also search untracked files.
-e PATTERN
Pattern argument; combine multiple -e with --and/--or/--not.
--help
Display help information.

INSTALL

sudo apt install git
copy
sudo dnf install git
copy
sudo pacman -S git
copy
sudo apk add git
copy
sudo zypper install git
copy
brew install git
copy
nix profile install nixpkgs#git
copy

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)

RESOURCES

Copied to clipboard
Kai