The twelfth batch
[git.git] / Documentation / git-grep.txt
blob0d0103c780af8c454db02a33c909343fb7bd51be
1 git-grep(1)
2 ===========
4 NAME
5 ----
6 git-grep - Print lines matching a pattern
9 SYNOPSIS
10 --------
11 [verse]
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]
15            [-P | --perl-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>]]
19            [-z | --null]
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>]
27            [--threads <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>...]
32            [--] [<pathspec>...]
34 DESCRIPTION
35 -----------
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.
42 OPTIONS
43 -------
44 --cached::
45         Instead of searching tracked files in the working tree, search
46         blobs registered in the index file.
48 --no-index::
49         Search files in the current directory that is not managed by Git.
51 --untracked::
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`.
59 --exclude-standard::
60         Do not pay attention to ignored files specified via the `.gitignore`
61         mechanism.  Only useful when searching files in the current directory
62         with `--no-index`.
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.
71 -a::
72 --text::
73         Process binary files as if they were text.
75 --textconv::
76         Honor textconv filter settings.
78 --no-textconv::
79         Do not honor textconv filter settings.
80         This is the default.
82 -i::
83 --ignore-case::
84         Ignore case differences between the patterns and the
85         files.
87 -I::
88         Don't match the pattern in binary files.
90 --max-depth <depth>::
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.
97 -r::
98 --recursive::
99         Same as `--max-depth=-1`; this is the default.
101 --no-recursive::
102         Same as `--max-depth=0`.
104 -w::
105 --word-regexp::
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).
110 -v::
111 --invert-match::
112         Select non-matching lines.
114 -h::
115 -H::
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
120         line.
122 --full-name::
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
126         top directory.
128 -E::
129 --extended-regexp::
130 -G::
131 --basic-regexp::
132         Use POSIX extended/basic regexp for patterns.  Default
133         is to use basic regexp.
135 -P::
136 --perl-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.
143 -F::
144 --fixed-strings::
145         Use fixed strings for patterns (don't interpret pattern
146         as a regex).
148 -n::
149 --line-number::
150         Prefix the line number to matching lines.
152 --column::
153         Prefix the 1-indexed byte-offset of the first match from the start of the
154         matching line.
156 -l::
157 --files-with-matches::
158 --name-only::
159 -L::
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`.
166 -O[<pager>]::
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]).
176 -z::
177 --null::
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]).
183 -o::
184 --only-matching::
185         Print only the matched (non-empty) parts of a matching line, with each such
186         part on a separate output line.
188 -c::
189 --count::
190         Instead of showing every matched line, show the number of
191         lines that match.
193 --color[=<when>]::
194         Show colored matches.
195         The value must be always (the default), never, or auto.
197 --no-color::
198         Turn off match highlighting, even when the configuration file
199         gives the default to color output.
200         Same as `--color=never`.
202 --break::
203         Print an empty line between matches from different files.
205 --heading::
206         Show the filename above the matches in that file instead of
207         at the start of each shown line.
209 -p::
210 --show-function::
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]).
217 -<num>::
218 -C <num>::
219 --context <num>::
220         Show <num> leading and trailing lines, and place a line
221         containing `--` between contiguous groups of matches.
223 -A <num>::
224 --after-context <num>::
225         Show <num> trailing lines, and place a line containing
226         `--` between contiguous groups of matches.
228 -B <num>::
229 --before-context <num>::
230         Show <num> leading lines, and place a line containing
231         `--` between contiguous groups of matches.
233 -W::
234 --function-context::
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]).
242 -m <num>::
243 --max-count <num>::
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
248         a non-zero status.
250 --threads <num>::
251         Number of grep worker threads to use.
252         See `grep.threads` in 'CONFIGURATION' for more information.
254 -f <file>::
255         Read patterns from <file>, one per line.
257 Passing the pattern via <file> allows for providing a search pattern
258 containing a \0.
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.
274 -e::
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
278         combined by 'or'.
280 --and::
281 --or::
282 --not::
283 ( ... )::
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
287         patterns.
289 --all-match::
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.
294 -q::
295 --quiet::
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.
299 <tree>...::
300         Instead of searching tracked files in the working tree, search
301         blobs in the given trees.
303 \--::
304         Signals the end of options; the rest of the parameters
305         are <pathspec> limiters.
307 <pathspec>...::
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].
314 EXAMPLES
315 --------
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
323         `PATH_MAX`.
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`.
332 NOTES ON THREADS
333 ----------------
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`.
343 CONFIGURATION
344 -------------
346 include::includes/cmd-config-section-all.txt[]
348 include::config/grep.txt[]
352 Part of the linkgit:git[1] suite