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 | --no-index | --untracked] | <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 Search files in the current directory that is not managed by Git.
52 In addition to searching in the tracked files in the working
53 tree, search also in untracked files.
55 --no-exclude-standard::
56 Also search in ignored files by not honoring the `.gitignore`
57 mechanism. Only useful with `--untracked`.
60 Do not pay attention to ignored files specified via the `.gitignore`
61 mechanism. Only useful when searching files in the current directory
64 --recurse-submodules::
65 Recursively search in each submodule that is active and
66 checked out in the repository. When used in combination with the
67 <tree> option the prefix of all submodule output will be the name of
68 the parent project's <tree> object. This option has no effect
69 if `--no-index` is given.
73 Process binary files as if they were text.
76 Honor textconv filter settings.
79 Do not honor textconv filter settings.
84 Ignore case differences between the patterns and the
88 Don't match the pattern in binary files.
91 For each <pathspec> given on command line, descend at most <depth>
92 levels of directories. A value of -1 means no limit.
93 This option is ignored if <pathspec> contains active wildcards.
94 In other words if "a*" matches a directory named "a*",
95 "*" is matched literally so --max-depth is still effective.
99 Same as `--max-depth=-1`; this is the default.
102 Same as `--max-depth=0`.
106 Match the pattern only at word boundary (either begin at the
107 beginning of a line, or preceded by a non-word character; end at
108 the end of a line or followed by a non-word character).
112 Select non-matching lines.
116 By default, the command shows the filename for each
117 match. `-h` option is used to suppress this output.
118 `-H` is there for completeness and does not do anything
119 except it overrides `-h` given earlier on the command
123 When run from a subdirectory, the command usually
124 outputs paths relative to the current directory. This
125 option forces paths to be output relative to the project
132 Use POSIX extended/basic regexp for patterns. Default
133 is to use basic regexp.
137 Use Perl-compatible regular expressions for patterns.
139 Support for these types of regular expressions is an optional
140 compile-time dependency. If Git wasn't compiled with support for them
141 providing this option will cause it to die.
145 Use fixed strings for patterns (don't interpret pattern
150 Prefix the line number to matching lines.
153 Prefix the 1-indexed byte-offset of the first match from the start of the
157 --files-with-matches::
160 --files-without-match::
161 Instead of showing every matched line, show only the
162 names of files that contain (or do not contain) matches.
163 For better compatibility with 'git diff', `--name-only` is a
164 synonym for `--files-with-matches`.
167 --open-files-in-pager[=<pager>]::
168 Open the matching files in the pager (not the output of 'grep').
169 If the pager happens to be "less" or "vi", and the user
170 specified only one pattern, the first file is positioned at
171 the first match automatically. The `pager` argument is
172 optional; if specified, it must be stuck to the option
173 without a space. If `pager` is unspecified, the default pager
174 will be used (see `core.pager` in linkgit:git-config[1]).
178 Use \0 as the delimiter for pathnames in the output, and print
179 them verbatim. Without this option, pathnames with "unusual"
180 characters are quoted as explained for the configuration
181 variable core.quotePath (see linkgit:git-config[1]).
185 Print only the matched (non-empty) parts of a matching line, with each such
186 part on a separate output line.
190 Instead of showing every matched line, show the number of
194 Show colored matches.
195 The value must be always (the default), never, or auto.
198 Turn off match highlighting, even when the configuration file
199 gives the default to color output.
200 Same as `--color=never`.
203 Print an empty line between matches from different files.
206 Show the filename above the matches in that file instead of
207 at the start of each shown line.
211 Show the preceding line that contains the function name of
212 the match, unless the matching line is a function name itself.
213 The name is determined in the same way as `git diff` works out
214 patch hunk headers (see 'Defining a custom hunk-header' in
215 linkgit:gitattributes[5]).
220 Show <num> leading and trailing lines, and place a line
221 containing `--` between contiguous groups of matches.
224 --after-context <num>::
225 Show <num> trailing lines, and place a line containing
226 `--` between contiguous groups of matches.
229 --before-context <num>::
230 Show <num> leading lines, and place a line containing
231 `--` between contiguous groups of matches.
235 Show the surrounding text from the previous line containing a
236 function name up to the one before the next function name,
237 effectively showing the whole function in which the match was
238 found. The function names are determined in the same way as
239 `git diff` works out patch hunk headers (see 'Defining a
240 custom hunk-header' in linkgit:gitattributes[5]).
244 Limit the amount of matches per file. When using the `-v` or
245 `--invert-match` option, the search stops after the specified
246 number of non-matches. A value of -1 will return unlimited
247 results (the default). A value of 0 will exit immediately with
251 Number of grep worker threads to use.
252 See `grep.threads` in 'CONFIGURATION' for more information.
255 Read patterns from <file>, one per line.
257 Passing the pattern via <file> allows for providing a search pattern
260 Not all pattern types support patterns containing \0. Git will error
261 out if a given pattern type can't support such a pattern. The
262 `--perl-regexp` pattern type when compiled against the PCRE v2 backend
263 has the widest support for these types of patterns.
265 In versions of Git before 2.23.0 patterns containing \0 would be
266 silently considered fixed. This was never documented, there were also
267 odd and undocumented interactions between e.g. non-ASCII patterns
268 containing \0 and `--ignore-case`.
270 In future versions we may learn to support patterns containing \0 for
271 more search backends, until then we'll die when the pattern type in
272 question doesn't support them.
275 The next parameter is the pattern. This option has to be
276 used for patterns starting with `-` and should be used in
277 scripts passing user input to grep. Multiple patterns are
284 Specify how multiple patterns are combined using Boolean
285 expressions. `--or` is the default operator. `--and` has
286 higher precedence than `--or`. `-e` has to be used for all
290 When giving multiple pattern expressions combined with `--or`,
291 this flag is specified to limit the match to files that
292 have lines to match all of them.
296 Do not output matched lines; instead, exit with status 0 when
297 there is a match and with non-zero status when there isn't.
300 Instead of searching tracked files in the working tree, search
301 blobs in the given trees.
304 Signals the end of options; the rest of the parameters
305 are <pathspec> limiters.
308 If given, limit the search to paths matching at least one pattern.
309 Both leading paths match and glob(7) patterns are supported.
311 For more details about the <pathspec> syntax, see the 'pathspec' entry
312 in linkgit:gitglossary[7].
317 `git grep 'time_t' -- '*.[ch]'`::
318 Looks for `time_t` in all tracked .c and .h files in the working
319 directory and its subdirectories.
321 `git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)`::
322 Looks for a line that has `#define` and either `MAX_PATH` or
325 `git grep --all-match -e NODE -e Unexpected`::
326 Looks for a line that has `NODE` or `Unexpected` in
327 files that have lines that match both.
329 `git grep solution -- :^Documentation`::
330 Looks for `solution`, excluding files in `Documentation`.
335 The `--threads` option (and the grep.threads configuration) will be ignored when
336 `--open-files-in-pager` is used, forcing a single-threaded execution.
338 When grepping the object store (with `--cached` or giving tree objects), running
339 with multiple threads might perform slower than single threaded if `--textconv`
340 is given and there are too many text conversions. So if you experience low
341 performance in this case, it might be desirable to use `--threads=1`.
346 include::includes/cmd-config-section-all.txt[]
348 include::config/grep.txt[]
352 Part of the linkgit:git[1] suite