commit-reach: plug minor memory leak after using is_descendant_of()
[git/raj.git] / Documentation / git-grep.txt
blobddb6acc0257ef25c415de9e63e4fb9bacd1728ea
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            [--threads <num>]
27            [-f <file>] [-e] <pattern>
28            [--and|--or|--not|(|)|-e <pattern>...]
29            [--recurse-submodules] [--parent-basename <basename>]
30            [ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
31            [--] [<pathspec>...]
33 DESCRIPTION
34 -----------
35 Look for specified patterns in the tracked files in the work tree, blobs
36 registered in the index file, or blobs in given tree objects.  Patterns
37 are lists of one or more search expressions separated by newline
38 characters.  An empty string as search expression matches all lines.
41 CONFIGURATION
42 -------------
44 grep.lineNumber::
45         If set to true, enable `-n` option by default.
47 grep.column::
48         If set to true, enable the `--column` option by default.
50 grep.patternType::
51         Set the default matching behavior. Using a value of 'basic', 'extended',
52         'fixed', or 'perl' will enable the `--basic-regexp`, `--extended-regexp`,
53         `--fixed-strings`, or `--perl-regexp` option accordingly, while the
54         value 'default' will return to the default matching behavior.
56 grep.extendedRegexp::
57         If set to true, enable `--extended-regexp` option by default. This
58         option is ignored when the `grep.patternType` option is set to a value
59         other than 'default'.
61 grep.threads::
62         Number of grep worker threads to use. If unset (or set to 0), Git will
63         use as many threads as the number of logical cores available.
65 grep.fullName::
66         If set to true, enable `--full-name` option by default.
68 grep.fallbackToNoIndex::
69         If set to true, fall back to git grep --no-index if git grep
70         is executed outside of a git repository.  Defaults to false.
73 OPTIONS
74 -------
75 --cached::
76         Instead of searching tracked files in the working tree, search
77         blobs registered in the index file.
79 --no-index::
80         Search files in the current directory that is not managed by Git.
82 --untracked::
83         In addition to searching in the tracked files in the working
84         tree, search also in untracked files.
86 --no-exclude-standard::
87         Also search in ignored files by not honoring the `.gitignore`
88         mechanism. Only useful with `--untracked`.
90 --exclude-standard::
91         Do not pay attention to ignored files specified via the `.gitignore`
92         mechanism.  Only useful when searching files in the current directory
93         with `--no-index`.
95 --recurse-submodules::
96         Recursively search in each submodule that has been initialized and
97         checked out in the repository.  When used in combination with the
98         <tree> option the prefix of all submodule output will be the name of
99         the parent project's <tree> object. This option has no effect
100         if `--no-index` is given.
102 -a::
103 --text::
104         Process binary files as if they were text.
106 --textconv::
107         Honor textconv filter settings.
109 --no-textconv::
110         Do not honor textconv filter settings.
111         This is the default.
113 -i::
114 --ignore-case::
115         Ignore case differences between the patterns and the
116         files.
118 -I::
119         Don't match the pattern in binary files.
121 --max-depth <depth>::
122         For each <pathspec> given on command line, descend at most <depth>
123         levels of directories. A value of -1 means no limit.
124         This option is ignored if <pathspec> contains active wildcards.
125         In other words if "a*" matches a directory named "a*",
126         "*" is matched literally so --max-depth is still effective.
128 -r::
129 --recursive::
130         Same as `--max-depth=-1`; this is the default.
132 --no-recursive::
133         Same as `--max-depth=0`.
135 -w::
136 --word-regexp::
137         Match the pattern only at word boundary (either begin at the
138         beginning of a line, or preceded by a non-word character; end at
139         the end of a line or followed by a non-word character).
141 -v::
142 --invert-match::
143         Select non-matching lines.
145 -h::
146 -H::
147         By default, the command shows the filename for each
148         match.  `-h` option is used to suppress this output.
149         `-H` is there for completeness and does not do anything
150         except it overrides `-h` given earlier on the command
151         line.
153 --full-name::
154         When run from a subdirectory, the command usually
155         outputs paths relative to the current directory.  This
156         option forces paths to be output relative to the project
157         top directory.
159 -E::
160 --extended-regexp::
161 -G::
162 --basic-regexp::
163         Use POSIX extended/basic regexp for patterns.  Default
164         is to use basic regexp.
166 -P::
167 --perl-regexp::
168         Use Perl-compatible regular expressions for patterns.
170 Support for these types of regular expressions is an optional
171 compile-time dependency. If Git wasn't compiled with support for them
172 providing this option will cause it to die.
174 -F::
175 --fixed-strings::
176         Use fixed strings for patterns (don't interpret pattern
177         as a regex).
179 -n::
180 --line-number::
181         Prefix the line number to matching lines.
183 --column::
184         Prefix the 1-indexed byte-offset of the first match from the start of the
185         matching line.
187 -l::
188 --files-with-matches::
189 --name-only::
190 -L::
191 --files-without-match::
192         Instead of showing every matched line, show only the
193         names of files that contain (or do not contain) matches.
194         For better compatibility with 'git diff', `--name-only` is a
195         synonym for `--files-with-matches`.
197 -O[<pager>]::
198 --open-files-in-pager[=<pager>]::
199         Open the matching files in the pager (not the output of 'grep').
200         If the pager happens to be "less" or "vi", and the user
201         specified only one pattern, the first file is positioned at
202         the first match automatically. The `pager` argument is
203         optional; if specified, it must be stuck to the option
204         without a space. If `pager` is unspecified, the default pager
205         will be used (see `core.pager` in linkgit:git-config[1]).
207 -z::
208 --null::
209         Output \0 instead of the character that normally follows a
210         file name.
212 -o::
213 --only-matching::
214         Print only the matched (non-empty) parts of a matching line, with each such
215         part on a separate output line.
217 -c::
218 --count::
219         Instead of showing every matched line, show the number of
220         lines that match.
222 --color[=<when>]::
223         Show colored matches.
224         The value must be always (the default), never, or auto.
226 --no-color::
227         Turn off match highlighting, even when the configuration file
228         gives the default to color output.
229         Same as `--color=never`.
231 --break::
232         Print an empty line between matches from different files.
234 --heading::
235         Show the filename above the matches in that file instead of
236         at the start of each shown line.
238 -p::
239 --show-function::
240         Show the preceding line that contains the function name of
241         the match, unless the matching line is a function name itself.
242         The name is determined in the same way as 'git diff' works out
243         patch hunk headers (see 'Defining a custom hunk-header' in
244         linkgit:gitattributes[5]).
246 -<num>::
247 -C <num>::
248 --context <num>::
249         Show <num> leading and trailing lines, and place a line
250         containing `--` between contiguous groups of matches.
252 -A <num>::
253 --after-context <num>::
254         Show <num> trailing lines, and place a line containing
255         `--` between contiguous groups of matches.
257 -B <num>::
258 --before-context <num>::
259         Show <num> leading lines, and place a line containing
260         `--` between contiguous groups of matches.
262 -W::
263 --function-context::
264         Show the surrounding text from the previous line containing a
265         function name up to the one before the next function name,
266         effectively showing the whole function in which the match was
267         found.
269 --threads <num>::
270         Number of grep worker threads to use.
271         See `grep.threads` in 'CONFIGURATION' for more information.
273 -f <file>::
274         Read patterns from <file>, one per line.
276 Passing the pattern via <file> allows for providing a search pattern
277 containing a \0.
279 Not all pattern types support patterns containing \0. Git will error
280 out if a given pattern type can't support such a pattern. The
281 `--perl-regexp` pattern type when compiled against the PCRE v2 backend
282 has the widest support for these types of patterns.
284 In versions of Git before 2.23.0 patterns containing \0 would be
285 silently considered fixed. This was never documented, there were also
286 odd and undocumented interactions between e.g. non-ASCII patterns
287 containing \0 and `--ignore-case`.
289 In future versions we may learn to support patterns containing \0 for
290 more search backends, until then we'll die when the pattern type in
291 question doesn't support them.
293 -e::
294         The next parameter is the pattern. This option has to be
295         used for patterns starting with `-` and should be used in
296         scripts passing user input to grep.  Multiple patterns are
297         combined by 'or'.
299 --and::
300 --or::
301 --not::
302 ( ... )::
303         Specify how multiple patterns are combined using Boolean
304         expressions.  `--or` is the default operator.  `--and` has
305         higher precedence than `--or`.  `-e` has to be used for all
306         patterns.
308 --all-match::
309         When giving multiple pattern expressions combined with `--or`,
310         this flag is specified to limit the match to files that
311         have lines to match all of them.
313 -q::
314 --quiet::
315         Do not output matched lines; instead, exit with status 0 when
316         there is a match and with non-zero status when there isn't.
318 <tree>...::
319         Instead of searching tracked files in the working tree, search
320         blobs in the given trees.
322 \--::
323         Signals the end of options; the rest of the parameters
324         are <pathspec> limiters.
326 <pathspec>...::
327         If given, limit the search to paths matching at least one pattern.
328         Both leading paths match and glob(7) patterns are supported.
330 For more details about the <pathspec> syntax, see the 'pathspec' entry
331 in linkgit:gitglossary[7].
333 EXAMPLES
334 --------
336 `git grep 'time_t' -- '*.[ch]'`::
337         Looks for `time_t` in all tracked .c and .h files in the working
338         directory and its subdirectories.
340 `git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)`::
341         Looks for a line that has `#define` and either `MAX_PATH` or
342         `PATH_MAX`.
344 `git grep --all-match -e NODE -e Unexpected`::
345         Looks for a line that has `NODE` or `Unexpected` in
346         files that have lines that match both.
348 `git grep solution -- :^Documentation`::
349         Looks for `solution`, excluding files in `Documentation`.
351 NOTES ON THREADS
352 ----------------
354 The `--threads` option (and the grep.threads configuration) will be ignored when
355 `--open-files-in-pager` is used, forcing a single-threaded execution.
357 When grepping the object store (with `--cached` or giving tree objects), running
358 with multiple threads might perform slower than single threaded if `--textconv`
359 is given and there're too many text conversions. So if you experience low
360 performance in this case, it might be desirable to use `--threads=1`.
364 Part of the linkgit:git[1] suite