Name

grep

Synopsis

git grep [-e] pattern [--cached|commit ...] [-- path ...]

Search the given commits for the given regular expression pattern(s).

If no commits are specified, normally searches the current work tree. With --cached, searches the index instead. If paths are specified, restricts the search to the given files or directories.

The main advantage of git grep over plain grep is that it ignores files (such as compiler output and editor backups) that have not been added with git add.

When multiple patterns are specified (using -e), the default combination is --or, unless --and or --not is specified.

Options

-e pattern
--and
--or
--not
(
)
-E (act like egrep)
-F (act like fgrep)
-i, --ignore-case
-v, --invert-match
-w, --word-regexp
-l, --files-with-matches
-L, --files-without-matches

Examples

To search the work tree for lines containing chicken without considering case:

$ git grep -i chicken

To search the top of branches test1 and test2 for lines containing alpha and either beta or gamma:

$ git grep -e alpha --and \( -e beta -e gamma \) test1 test2

To search only the mylib directory for files starting with the word chicken:

$ git grep '^chicken' -- mylib

Get Linux in a Nutshell, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.