O'Reilly logo

Linux in a Nutshell, 6th Edition by Robert Love, Stephen Figgins, Ellen Siever, Arnold Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required