]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-grep.txt
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / Documentation / git-grep.txt
CommitLineData
2aba319a
JH
1git-grep(1)
2===========
2aba319a
JH
3
4NAME
5----
5001422d 6git-grep - Print lines matching a pattern
2aba319a
JH
7
8
9SYNOPSIS
10--------
1c57119c 11[verse]
335ec3bf 12'git grep' [-a | --text] [-I] [--textconv] [-i | --ignore-case] [-w | --word-regexp]
17fd965d 13 [-v | --invert-match] [-h|-H] [--full-name]
cf0d720b 14 [-E | --extended-regexp] [-G | --basic-regexp]
63e7e9d8 15 [-P | --perl-regexp]
a449f27f 16 [-F | --fixed-strings] [-n | --line-number] [--column]
cf0d720b 17 [-l | --files-with-matches] [-L | --files-without-match]
0af88c15 18 [(-O | --open-files-in-pager) [<pager>]]
83caecca 19 [-z | --null]
9d8db06e 20 [ -o | --only-matching ] [-c | --count] [--all-match] [-q | --quiet]
0a09e5ed 21 [--max-depth <depth>] [--[no-]recursive]
73e9da01 22 [--color[=<when>] | --no-color]
a12c6b01 23 [--break] [--heading] [-p | --show-function]
1c57119c 24 [-A <post-context>] [-B <pre-context>] [-C <context>]
a12c6b01 25 [-W | --function-context]
68437ede 26 [(-m | --max-count) <num>]
89f09dd3 27 [--threads <num>]
cf0d720b 28 [-f <file>] [-e] <pattern>
ec2537be 29 [--and|--or|--not|(|)|-e <pattern>...]
74ed4371 30 [--recurse-submodules] [--parent-basename <basename>]
0460ed2c 31 [ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
3ac44408 32 [--] [<pathspec>...]
2aba319a
JH
33
34DESCRIPTION
35-----------
3ac44408 36Look for specified patterns in the tracked files in the work tree, blobs
526a858a
RS
37registered in the index file, or blobs in given tree objects. Patterns
38are lists of one or more search expressions separated by newline
39characters. An empty string as search expression matches all lines.
2aba319a
JH
40
41
42OPTIONS
43-------
1c57119c 44--cached::
2e48fcdb
ML
45 Instead of searching tracked files in the working tree, search
46 blobs registered in the index file.
47
48--no-index::
2de9b711 49 Search files in the current directory that is not managed by Git.
1c57119c 50
0a93fb8a
JH
51--untracked::
52 In addition to searching in the tracked files in the working
53 tree, search also in untracked files.
54
55--no-exclude-standard::
56 Also search in ignored files by not honoring the `.gitignore`
57 mechanism. Only useful with `--untracked`.
58
59--exclude-standard::
8d75a1d1 60 Do not pay attention to ignored files specified via the `.gitignore`
0a93fb8a
JH
61 mechanism. Only useful when searching files in the current directory
62 with `--no-index`.
1c57119c 63
0281e487 64--recurse-submodules::
acbfae32 65 Recursively search in each submodule that is active and
74ed4371
BW
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
c56c48dd
PB
68 the parent project's <tree> object. This option has no effect
69 if `--no-index` is given.
74ed4371 70
3240240f
SB
71-a::
72--text::
1c57119c
JH
73 Process binary files as if they were text.
74
335ec3bf
JK
75--textconv::
76 Honor textconv filter settings.
77
78--no-textconv::
79 Do not honor textconv filter settings.
80 This is the default.
81
3240240f
SB
82-i::
83--ignore-case::
1c57119c
JH
84 Ignore case differences between the patterns and the
85 files.
86
cf0d720b
AR
87-I::
88 Don't match the pattern in binary files.
89
a91f453f 90--max-depth <depth>::
3ac44408 91 For each <pathspec> given on command line, descend at most <depth>
0a09e5ed 92 levels of directories. A value of -1 means no limit.
8c69c1f9
NTND
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.
a91f453f 96
0a09e5ed
RS
97-r::
98--recursive::
99 Same as `--max-depth=-1`; this is the default.
100
101--no-recursive::
102 Same as `--max-depth=0`.
103
3240240f
SB
104-w::
105--word-regexp::
1c57119c
JH
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).
109
3240240f
SB
110-v::
111--invert-match::
1c57119c
JH
112 Select non-matching lines.
113
3240240f
SB
114-h::
115-H::
17fd965d
JH
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.
121
0d042fec
JH
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.
127
3240240f
SB
128-E::
129--extended-regexp::
130-G::
131--basic-regexp::
1c57119c
JH
132 Use POSIX extended/basic regexp for patterns. Default
133 is to use basic regexp.
5b2bcc7b 134
63e7e9d8
MK
135-P::
136--perl-regexp::
d048cb13
ÆAB
137 Use Perl-compatible regular expressions for patterns.
138+
139Support for these types of regular expressions is an optional
140compile-time dependency. If Git wasn't compiled with support for them
141providing this option will cause it to die.
63e7e9d8 142
3240240f
SB
143-F::
144--fixed-strings::
cf0d720b
AR
145 Use fixed strings for patterns (don't interpret pattern
146 as a regex).
147
1c57119c 148-n::
7d6cb10b 149--line-number::
1c57119c 150 Prefix the line number to matching lines.
2aba319a 151
a449f27f
TB
152--column::
153 Prefix the 1-indexed byte-offset of the first match from the start of the
154 matching line.
155
3240240f
SB
156-l::
157--files-with-matches::
158--name-only::
159-L::
160--files-without-match::
1c57119c
JH
161 Instead of showing every matched line, show only the
162 names of files that contain (or do not contain) matches.
bfb8306d
ML
163 For better compatibility with 'git diff', `--name-only` is a
164 synonym for `--files-with-matches`.
2aba319a 165
318ca615
MM
166-O[<pager>]::
167--open-files-in-pager[=<pager>]::
678e484b
JS
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
2b594bf9
MM
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]).
678e484b 175
83caecca
RZ
176-z::
177--null::
45115d84
MT
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
49fbf9ed 181 variable core.quotePath (see linkgit:git-config[1]).
83caecca 182
9d8db06e
TB
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.
187
3240240f
SB
188-c::
189--count::
1c57119c
JH
190 Instead of showing every matched line, show the number of
191 lines that match.
192
73e9da01 193--color[=<when>]::
7e8f59d5 194 Show colored matches.
73e9da01 195 The value must be always (the default), never, or auto.
7e8f59d5
RS
196
197--no-color::
198 Turn off match highlighting, even when the configuration file
199 gives the default to color output.
73e9da01 200 Same as `--color=never`.
7e8f59d5 201
a8f0e764
RS
202--break::
203 Print an empty line between matches from different files.
204
1d84f72e
RS
205--heading::
206 Show the filename above the matches in that file instead of
207 at the start of each shown line.
208
2944e4e6
RS
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.
0cce88f1 213 The name is determined in the same way as `git diff` works out
60ecac98
RS
214 patch hunk headers (see 'Defining a custom hunk-header' in
215 linkgit:gitattributes[5]).
2944e4e6 216
317f63c2
RS
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.
222
223-A <num>::
224--after-context <num>::
225 Show <num> trailing lines, and place a line containing
226 `--` between contiguous groups of matches.
227
228-B <num>::
229--before-context <num>::
230 Show <num> leading lines, and place a line containing
231 `--` between contiguous groups of matches.
232
ba8ea749 233-W::
317f63c2 234--function-context::
ba8ea749
RS
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
0cce88f1
PB
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]).
ba8ea749 241
68437ede
CL
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.
249
89f09dd3
VL
250--threads <num>::
251 Number of grep worker threads to use.
252 See `grep.threads` in 'CONFIGURATION' for more information.
253
1c57119c
JH
254-f <file>::
255 Read patterns from <file>, one per line.
25754125
ÆAB
256+
257Passing the pattern via <file> allows for providing a search pattern
258containing a \0.
259+
260Not all pattern types support patterns containing \0. Git will error
261out if a given pattern type can't support such a pattern. The
262`--perl-regexp` pattern type when compiled against the PCRE v2 backend
263has the widest support for these types of patterns.
264+
265In versions of Git before 2.23.0 patterns containing \0 would be
266silently considered fixed. This was never documented, there were also
267odd and undocumented interactions between e.g. non-ASCII patterns
268containing \0 and `--ignore-case`.
269+
270In future versions we may learn to support patterns containing \0 for
271more search backends, until then we'll die when the pattern type in
272question doesn't support them.
1c57119c 273
3acb27b6
ML
274-e::
275 The next parameter is the pattern. This option has to be
bfb8306d 276 used for patterns starting with `-` and should be used in
fba0cbd9
ML
277 scripts passing user input to grep. Multiple patterns are
278 combined by 'or'.
279
3240240f
SB
280--and::
281--or::
282--not::
283( ... )::
23bfbb81 284 Specify how multiple patterns are combined using Boolean
fba0cbd9
ML
285 expressions. `--or` is the default operator. `--and` has
286 higher precedence than `--or`. `-e` has to be used for all
287 patterns.
3acb27b6 288
0ab7befa
JH
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.
293
4ff61c21
JH
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.
298
bfb8306d 299<tree>...::
2e48fcdb
ML
300 Instead of searching tracked files in the working tree, search
301 blobs in the given trees.
1c57119c 302
fba0cbd9 303\--::
1c57119c 304 Signals the end of options; the rest of the parameters
3ac44408 305 are <pathspec> limiters.
2aba319a 306
3ac44408
ML
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.
93dbefb3
MR
310+
311For more details about the <pathspec> syntax, see the 'pathspec' entry
312in linkgit:gitglossary[7].
2aba319a 313
76a8788c 314EXAMPLES
04416018 315--------
fba0cbd9 316
6cf378f0 317`git grep 'time_t' -- '*.[ch]'`::
3ac44408
ML
318 Looks for `time_t` in all tracked .c and .h files in the working
319 directory and its subdirectories.
320
6cf378f0 321`git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)`::
fba0cbd9
ML
322 Looks for a line that has `#define` and either `MAX_PATH` or
323 `PATH_MAX`.
324
5d2fc913 325`git grep --all-match -e NODE -e Unexpected`::
0ab7befa
JH
326 Looks for a line that has `NODE` or `Unexpected` in
327 files that have lines that match both.
328
93dbefb3
MR
329`git grep solution -- :^Documentation`::
330 Looks for `solution`, excluding files in `Documentation`.
331
1184a95e
MT
332NOTES ON THREADS
333----------------
334
335The `--threads` option (and the grep.threads configuration) will be ignored when
336`--open-files-in-pager` is used, forcing a single-threaded execution.
337
338When grepping the object store (with `--cached` or giving tree objects), running
339with multiple threads might perform slower than single threaded if `--textconv`
340is given and there're too many text conversions. So if you experience low
341performance in this case, it might be desirable to use `--threads=1`.
342
414abf15
ÆAB
343CONFIGURATION
344-------------
345
a2811dd7 346include::includes/cmd-config-section-all.txt[]
414abf15 347
a2811dd7 348include::config/grep.txt[]
414abf15 349
2aba319a
JH
350GIT
351---
9e1f0a85 352Part of the linkgit:git[1] suite