Documentation / git-grep.txton commit Merge branch 'maint' (61e8aaf)
   1git-grep(1)
   2===========
   3
   4NAME
   5----
   6git-grep - Print lines matching a pattern
   7
   8
   9SYNOPSIS
  10--------
  11[verse]
  12'git grep' [-a | --text] [-I] [-i | --ignore-case] [-w | --word-regexp]
  13           [-v | --invert-match] [-h|-H] [--full-name]
  14           [-E | --extended-regexp] [-G | --basic-regexp]
  15           [-F | --fixed-strings] [-n]
  16           [-l | --files-with-matches] [-L | --files-without-match]
  17           [(-O | --open-files-in-pager) [<pager>]]
  18           [-z | --null]
  19           [-c | --count] [--all-match] [-q | --quiet]
  20           [--max-depth <depth>]
  21           [--color[=<when>] | --no-color]
  22           [-A <post-context>] [-B <pre-context>] [-C <context>]
  23           [-f <file>] [-e] <pattern>
  24           [--and|--or|--not|(|)|-e <pattern>...]
  25           [--cached | --no-index | <tree>...]
  26           [--] [<pathspec>...]
  27
  28DESCRIPTION
  29-----------
  30Look for specified patterns in the tracked files in the work tree, blobs
  31registered in the index file, or blobs in given tree objects.
  32
  33
  34OPTIONS
  35-------
  36--cached::
  37        Instead of searching tracked files in the working tree, search
  38        blobs registered in the index file.
  39
  40--no-index::
  41        Search files in the current directory, not just those tracked by git.
  42
  43-a::
  44--text::
  45        Process binary files as if they were text.
  46
  47-i::
  48--ignore-case::
  49        Ignore case differences between the patterns and the
  50        files.
  51
  52-I::
  53        Don't match the pattern in binary files.
  54
  55--max-depth <depth>::
  56        For each <pathspec> given on command line, descend at most <depth>
  57        levels of directories. A negative value means no limit.
  58
  59-w::
  60--word-regexp::
  61        Match the pattern only at word boundary (either begin at the
  62        beginning of a line, or preceded by a non-word character; end at
  63        the end of a line or followed by a non-word character).
  64
  65-v::
  66--invert-match::
  67        Select non-matching lines.
  68
  69-h::
  70-H::
  71        By default, the command shows the filename for each
  72        match.  `-h` option is used to suppress this output.
  73        `-H` is there for completeness and does not do anything
  74        except it overrides `-h` given earlier on the command
  75        line.
  76
  77--full-name::
  78        When run from a subdirectory, the command usually
  79        outputs paths relative to the current directory.  This
  80        option forces paths to be output relative to the project
  81        top directory.
  82
  83-E::
  84--extended-regexp::
  85-G::
  86--basic-regexp::
  87        Use POSIX extended/basic regexp for patterns.  Default
  88        is to use basic regexp.
  89
  90-F::
  91--fixed-strings::
  92        Use fixed strings for patterns (don't interpret pattern
  93        as a regex).
  94
  95-n::
  96--line-number::
  97        Prefix the line number to matching lines.
  98
  99-l::
 100--files-with-matches::
 101--name-only::
 102-L::
 103--files-without-match::
 104        Instead of showing every matched line, show only the
 105        names of files that contain (or do not contain) matches.
 106        For better compatibility with 'git diff', `--name-only` is a
 107        synonym for `--files-with-matches`.
 108
 109-O [<pager>]::
 110--open-files-in-pager [<pager>]::
 111        Open the matching files in the pager (not the output of 'grep').
 112        If the pager happens to be "less" or "vi", and the user
 113        specified only one pattern, the first file is positioned at
 114        the first match automatically.
 115
 116-z::
 117--null::
 118        Output \0 instead of the character that normally follows a
 119        file name.
 120
 121-c::
 122--count::
 123        Instead of showing every matched line, show the number of
 124        lines that match.
 125
 126--color[=<when>]::
 127        Show colored matches.
 128        The value must be always (the default), never, or auto.
 129
 130--no-color::
 131        Turn off match highlighting, even when the configuration file
 132        gives the default to color output.
 133        Same as `--color=never`.
 134
 135-[ABC] <context>::
 136        Show `context` trailing (`A` -- after), or leading (`B`
 137        -- before), or both (`C` -- context) lines, and place a
 138        line containing `--` between contiguous groups of
 139        matches.
 140
 141-<num>::
 142        A shortcut for specifying `-C<num>`.
 143
 144-p::
 145--show-function::
 146        Show the preceding line that contains the function name of
 147        the match, unless the matching line is a function name itself.
 148        The name is determined in the same way as 'git diff' works out
 149        patch hunk headers (see 'Defining a custom hunk-header' in
 150        linkgit:gitattributes[5]).
 151
 152-f <file>::
 153        Read patterns from <file>, one per line.
 154
 155-e::
 156        The next parameter is the pattern. This option has to be
 157        used for patterns starting with `-` and should be used in
 158        scripts passing user input to grep.  Multiple patterns are
 159        combined by 'or'.
 160
 161--and::
 162--or::
 163--not::
 164( ... )::
 165        Specify how multiple patterns are combined using Boolean
 166        expressions.  `--or` is the default operator.  `--and` has
 167        higher precedence than `--or`.  `-e` has to be used for all
 168        patterns.
 169
 170--all-match::
 171        When giving multiple pattern expressions combined with `--or`,
 172        this flag is specified to limit the match to files that
 173        have lines to match all of them.
 174
 175-q::
 176--quiet::
 177        Do not output matched lines; instead, exit with status 0 when
 178        there is a match and with non-zero status when there isn't.
 179
 180<tree>...::
 181        Instead of searching tracked files in the working tree, search
 182        blobs in the given trees.
 183
 184\--::
 185        Signals the end of options; the rest of the parameters
 186        are <pathspec> limiters.
 187
 188<pathspec>...::
 189        If given, limit the search to paths matching at least one pattern.
 190        Both leading paths match and glob(7) patterns are supported.
 191
 192Examples
 193--------
 194
 195git grep {apostrophe}time_t{apostrophe} \-- {apostrophe}*.[ch]{apostrophe}::
 196        Looks for `time_t` in all tracked .c and .h files in the working
 197        directory and its subdirectories.
 198
 199git grep -e {apostrophe}#define{apostrophe} --and \( -e MAX_PATH -e PATH_MAX \)::
 200        Looks for a line that has `#define` and either `MAX_PATH` or
 201        `PATH_MAX`.
 202
 203git grep --all-match -e NODE -e Unexpected::
 204        Looks for a line that has `NODE` or `Unexpected` in
 205        files that have lines that match both.
 206
 207GIT
 208---
 209Part of the linkgit:git[1] suite