6 git-grep - Print lines matching a pattern
12 'git grep' [-a | --text] [-I] [--textconv] [-i | --ignore-case] [-w | --word-regexp]
13 [-v | --invert-match] [-h|-H] [--full-name]
14 [-E | --extended-regexp] [-G | --basic-regexp]
16 [-F | --fixed-strings] [-n | --line-number] [--column]
17 [-l | --files-with-matches] [-L | --files-without-match]
18 [(-O | --open-files-in-pager) [<pager>]]
20 [ -o | --only-matching ] [-c | --count] [--all-match] [-q | --quiet]
21 [--max-depth <depth>] [--[no-]recursive]
22 [--color[=<when>] | --no-color]
23 [--break] [--heading] [-p | --show-function]
24 [-A <post-context>] [-B <pre-context>] [-C <context>]
25 [-W | --function-context]
26 [(-m | --max-count) <num>]
28 [-f <file>] [-e] <pattern>
29 [--and|--or|--not|(|)|-e <pattern>...]
30 [--recurse-submodules] [--parent-basename <basename>]
31 [ [--[no-]exclude-standard] [--cached | --untracked | --no-index] | <tree>...]
36 Look for specified patterns in the tracked files in the work tree, blobs
37 registered in the index file, or blobs in given tree objects. Patterns
38 are lists of one or more search expressions separated by newline
39 characters. An empty string as search expression matches all lines.
45 Instead of searching tracked files in the working tree, search
46 blobs registered in the index file.
49 In addition to searching in the tracked files in the working
50 tree, search also in untracked files.
53 Search files in the current directory that is not managed by Git,
54 or by ignoring that the current directory is managed by Git. This
55 is rather similar to running the regular `grep(1)` utility with its
56 `-r` option specified, but with some additional benefits, such as
57 using pathspec patterns to limit paths; see the 'pathspec' entry
58 in linkgit:gitglossary[7] for more information.
60 This option cannot be used together with `--cached` or `--untracked`.
61 See also `grep.fallbackToNoIndex` in 'CONFIGURATION' below.
63 --no-exclude-standard::
64 Also search in ignored files by not honoring the `.gitignore`
65 mechanism. Only useful with `--untracked`.
68 Do not pay attention to ignored files specified via the `.gitignore`
69 mechanism. Only useful when searching files in the current directory
72 --recurse-submodules::
73 Recursively search in each submodule that is active and
74 checked out in the repository. When used in combination with the
75 _<tree>_ option the prefix of all submodule output will be the name of
76 the parent project's _<tree>_ object. This option cannot be used together
77 with `--untracked`, and it has no effect if `--no-index` is specified.
81 Process binary files as if they were text.
84 Honor textconv filter settings.
87 Do not honor textconv filter settings.
92 Ignore case differences between the patterns and the
96 Don't match the pattern in binary files.
99 For each <pathspec> given on command line, descend at most <depth>
100 levels of directories. A value of -1 means no limit.
101 This option is ignored if <pathspec> contains active wildcards.
102 In other words if "a*" matches a directory named "a*",
103 "*" is matched literally so --max-depth is still effective.
107 Same as `--max-depth=-1`; this is the default.
110 Same as `--max-depth=0`.
114 Match the pattern only at word boundary (either begin at the
115 beginning of a line, or preceded by a non-word character; end at
116 the end of a line or followed by a non-word character).
120 Select non-matching lines.
124 By default, the command shows the filename for each
125 match. `-h` option is used to suppress this output.
126 `-H` is there for completeness and does not do anything
127 except it overrides `-h` given earlier on the command
131 When run from a subdirectory, the command usually
132 outputs paths relative to the current directory. This
133 option forces paths to be output relative to the project
140 Use POSIX extended/basic regexp for patterns. Default
141 is to use basic regexp.
145 Use Perl-compatible regular expressions for patterns.
147 Support for these types of regular expressions is an optional
148 compile-time dependency. If Git wasn't compiled with support for them
149 providing this option will cause it to die.
153 Use fixed strings for patterns (don't interpret pattern
158 Prefix the line number to matching lines.
161 Prefix the 1-indexed byte-offset of the first match from the start of the
165 --files-with-matches::
168 --files-without-match::
169 Instead of showing every matched line, show only the
170 names of files that contain (or do not contain) matches.
171 For better compatibility with 'git diff', `--name-only` is a
172 synonym for `--files-with-matches`.
175 --open-files-in-pager[=<pager>]::
176 Open the matching files in the pager (not the output of 'grep').
177 If the pager happens to be "less" or "vi", and the user
178 specified only one pattern, the first file is positioned at
179 the first match automatically. The `pager` argument is
180 optional; if specified, it must be stuck to the option
181 without a space. If `pager` is unspecified, the default pager
182 will be used (see `core.pager` in linkgit:git-config[1]).
186 Use \0 as the delimiter for pathnames in the output, and print
187 them verbatim. Without this option, pathnames with "unusual"
188 characters are quoted as explained for the configuration
189 variable `core.quotePath` (see linkgit:git-config[1]).
193 Print only the matched (non-empty) parts of a matching line, with each such
194 part on a separate output line.
198 Instead of showing every matched line, show the number of
202 Show colored matches.
203 The value must be always (the default), never, or auto.
206 Turn off match highlighting, even when the configuration file
207 gives the default to color output.
208 Same as `--color=never`.
211 Print an empty line between matches from different files.
214 Show the filename above the matches in that file instead of
215 at the start of each shown line.
219 Show the preceding line that contains the function name of
220 the match, unless the matching line is a function name itself.
221 The name is determined in the same way as `git diff` works out
222 patch hunk headers (see 'Defining a custom hunk-header' in
223 linkgit:gitattributes[5]).
228 Show <num> leading and trailing lines, and place a line
229 containing `--` between contiguous groups of matches.
232 --after-context <num>::
233 Show <num> trailing lines, and place a line containing
234 `--` between contiguous groups of matches.
237 --before-context <num>::
238 Show <num> leading lines, and place a line containing
239 `--` between contiguous groups of matches.
243 Show the surrounding text from the previous line containing a
244 function name up to the one before the next function name,
245 effectively showing the whole function in which the match was
246 found. The function names are determined in the same way as
247 `git diff` works out patch hunk headers (see 'Defining a
248 custom hunk-header' in linkgit:gitattributes[5]).
252 Limit the amount of matches per file. When using the `-v` or
253 `--invert-match` option, the search stops after the specified
254 number of non-matches. A value of -1 will return unlimited
255 results (the default). A value of 0 will exit immediately with
259 Number of `grep` worker threads to use. See 'NOTES ON THREADS'
260 and `grep.threads` in 'CONFIGURATION' for more information.
263 Read patterns from <file>, one per line.
265 Passing the pattern via <file> allows for providing a search pattern
268 Not all pattern types support patterns containing \0. Git will error
269 out if a given pattern type can't support such a pattern. The
270 `--perl-regexp` pattern type when compiled against the PCRE v2 backend
271 has the widest support for these types of patterns.
273 In versions of Git before 2.23.0 patterns containing \0 would be
274 silently considered fixed. This was never documented, there were also
275 odd and undocumented interactions between e.g. non-ASCII patterns
276 containing \0 and `--ignore-case`.
278 In future versions we may learn to support patterns containing \0 for
279 more search backends, until then we'll die when the pattern type in
280 question doesn't support them.
283 The next parameter is the pattern. This option has to be
284 used for patterns starting with `-` and should be used in
285 scripts passing user input to grep. Multiple patterns are
292 Specify how multiple patterns are combined using Boolean
293 expressions. `--or` is the default operator. `--and` has
294 higher precedence than `--or`. `-e` has to be used for all
298 When giving multiple pattern expressions combined with `--or`,
299 this flag is specified to limit the match to files that
300 have lines to match all of them.
304 Do not output matched lines; instead, exit with status 0 when
305 there is a match and with non-zero status when there isn't.
308 Instead of searching tracked files in the working tree, search
309 blobs in the given trees.
312 Signals the end of options; the rest of the parameters
313 are <pathspec> limiters.
316 If given, limit the search to paths matching at least one pattern.
317 Both leading paths match and glob(7) patterns are supported.
319 For more details about the <pathspec> syntax, see the 'pathspec' entry
320 in linkgit:gitglossary[7].
325 `git grep 'time_t' -- '*.[ch]'`::
326 Looks for `time_t` in all tracked .c and .h files in the working
327 directory and its subdirectories.
329 `git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)`::
330 Looks for a line that has `#define` and either `MAX_PATH` or
333 `git grep --all-match -e NODE -e Unexpected`::
334 Looks for a line that has `NODE` or `Unexpected` in
335 files that have lines that match both.
337 `git grep solution -- :^Documentation`::
338 Looks for `solution`, excluding files in `Documentation`.
343 The `--threads` option (and the `grep.threads` configuration) will be ignored when
344 `--open-files-in-pager` is used, forcing a single-threaded execution.
346 When grepping the object store (with `--cached` or giving tree objects), running
347 with multiple threads might perform slower than single-threaded if `--textconv`
348 is given and there are too many text conversions. Thus, if low performance is
349 experienced in this case, it might be desirable to use `--threads=1`.
354 include::includes/cmd-config-section-all.txt[]
356 include::config/grep.txt[]
360 Part of the linkgit:git[1] suite