]>
Commit | Line | Data |
---|---|---|
2aba319a JH |
1 | git-grep(1) |
2 | =========== | |
2aba319a JH |
3 | |
4 | NAME | |
5 | ---- | |
5001422d | 6 | git-grep - Print lines matching a pattern |
2aba319a JH |
7 | |
8 | ||
9 | SYNOPSIS | |
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>] |
6e9ef296 | 31 | [ [--[no-]exclude-standard] [--cached | --untracked | --no-index] | <tree>...] |
3ac44408 | 32 | [--] [<pathspec>...] |
2aba319a JH |
33 | |
34 | DESCRIPTION | |
35 | ----------- | |
3ac44408 | 36 | Look for specified patterns in the tracked files in the work tree, blobs |
526a858a RS |
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. | |
2aba319a JH |
40 | |
41 | ||
42 | OPTIONS | |
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 | ||
0a93fb8a JH |
48 | --untracked:: |
49 | In addition to searching in the tracked files in the working | |
50 | tree, search also in untracked files. | |
51 | ||
6e9ef296 DS |
52 | --no-index:: |
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. | |
59 | + | |
60 | This option cannot be used together with `--cached` or `--untracked`. | |
61 | See also `grep.fallbackToNoIndex` in 'CONFIGURATION' below. | |
62 | ||
0a93fb8a JH |
63 | --no-exclude-standard:: |
64 | Also search in ignored files by not honoring the `.gitignore` | |
65 | mechanism. Only useful with `--untracked`. | |
66 | ||
67 | --exclude-standard:: | |
8d75a1d1 | 68 | Do not pay attention to ignored files specified via the `.gitignore` |
0a93fb8a JH |
69 | mechanism. Only useful when searching files in the current directory |
70 | with `--no-index`. | |
1c57119c | 71 | |
0281e487 | 72 | --recurse-submodules:: |
acbfae32 | 73 | Recursively search in each submodule that is active and |
74ed4371 | 74 | checked out in the repository. When used in combination with the |
4a9357a1 DS |
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. | |
74ed4371 | 78 | |
3240240f SB |
79 | -a:: |
80 | --text:: | |
1c57119c JH |
81 | Process binary files as if they were text. |
82 | ||
335ec3bf JK |
83 | --textconv:: |
84 | Honor textconv filter settings. | |
85 | ||
86 | --no-textconv:: | |
87 | Do not honor textconv filter settings. | |
88 | This is the default. | |
89 | ||
3240240f SB |
90 | -i:: |
91 | --ignore-case:: | |
1c57119c JH |
92 | Ignore case differences between the patterns and the |
93 | files. | |
94 | ||
cf0d720b AR |
95 | -I:: |
96 | Don't match the pattern in binary files. | |
97 | ||
a91f453f | 98 | --max-depth <depth>:: |
3ac44408 | 99 | For each <pathspec> given on command line, descend at most <depth> |
0a09e5ed | 100 | levels of directories. A value of -1 means no limit. |
8c69c1f9 NTND |
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. | |
a91f453f | 104 | |
0a09e5ed RS |
105 | -r:: |
106 | --recursive:: | |
107 | Same as `--max-depth=-1`; this is the default. | |
108 | ||
109 | --no-recursive:: | |
110 | Same as `--max-depth=0`. | |
111 | ||
3240240f SB |
112 | -w:: |
113 | --word-regexp:: | |
1c57119c JH |
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). | |
117 | ||
3240240f SB |
118 | -v:: |
119 | --invert-match:: | |
1c57119c JH |
120 | Select non-matching lines. |
121 | ||
3240240f SB |
122 | -h:: |
123 | -H:: | |
17fd965d JH |
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 | |
128 | line. | |
129 | ||
0d042fec JH |
130 | --full-name:: |
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 | |
134 | top directory. | |
135 | ||
3240240f SB |
136 | -E:: |
137 | --extended-regexp:: | |
138 | -G:: | |
139 | --basic-regexp:: | |
1c57119c JH |
140 | Use POSIX extended/basic regexp for patterns. Default |
141 | is to use basic regexp. | |
5b2bcc7b | 142 | |
63e7e9d8 MK |
143 | -P:: |
144 | --perl-regexp:: | |
d048cb13 ÆAB |
145 | Use Perl-compatible regular expressions for patterns. |
146 | + | |
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. | |
63e7e9d8 | 150 | |
3240240f SB |
151 | -F:: |
152 | --fixed-strings:: | |
cf0d720b AR |
153 | Use fixed strings for patterns (don't interpret pattern |
154 | as a regex). | |
155 | ||
1c57119c | 156 | -n:: |
7d6cb10b | 157 | --line-number:: |
1c57119c | 158 | Prefix the line number to matching lines. |
2aba319a | 159 | |
a449f27f TB |
160 | --column:: |
161 | Prefix the 1-indexed byte-offset of the first match from the start of the | |
162 | matching line. | |
163 | ||
3240240f SB |
164 | -l:: |
165 | --files-with-matches:: | |
166 | --name-only:: | |
167 | -L:: | |
168 | --files-without-match:: | |
1c57119c JH |
169 | Instead of showing every matched line, show only the |
170 | names of files that contain (or do not contain) matches. | |
bfb8306d ML |
171 | For better compatibility with 'git diff', `--name-only` is a |
172 | synonym for `--files-with-matches`. | |
2aba319a | 173 | |
318ca615 MM |
174 | -O[<pager>]:: |
175 | --open-files-in-pager[=<pager>]:: | |
678e484b JS |
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 | |
2b594bf9 MM |
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]). | |
678e484b | 183 | |
83caecca RZ |
184 | -z:: |
185 | --null:: | |
45115d84 MT |
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 | |
4a9357a1 | 189 | variable `core.quotePath` (see linkgit:git-config[1]). |
83caecca | 190 | |
9d8db06e TB |
191 | -o:: |
192 | --only-matching:: | |
193 | Print only the matched (non-empty) parts of a matching line, with each such | |
194 | part on a separate output line. | |
195 | ||
3240240f SB |
196 | -c:: |
197 | --count:: | |
1c57119c JH |
198 | Instead of showing every matched line, show the number of |
199 | lines that match. | |
200 | ||
73e9da01 | 201 | --color[=<when>]:: |
7e8f59d5 | 202 | Show colored matches. |
73e9da01 | 203 | The value must be always (the default), never, or auto. |
7e8f59d5 RS |
204 | |
205 | --no-color:: | |
206 | Turn off match highlighting, even when the configuration file | |
207 | gives the default to color output. | |
73e9da01 | 208 | Same as `--color=never`. |
7e8f59d5 | 209 | |
a8f0e764 RS |
210 | --break:: |
211 | Print an empty line between matches from different files. | |
212 | ||
1d84f72e RS |
213 | --heading:: |
214 | Show the filename above the matches in that file instead of | |
215 | at the start of each shown line. | |
216 | ||
2944e4e6 RS |
217 | -p:: |
218 | --show-function:: | |
219 | Show the preceding line that contains the function name of | |
220 | the match, unless the matching line is a function name itself. | |
0cce88f1 | 221 | The name is determined in the same way as `git diff` works out |
60ecac98 RS |
222 | patch hunk headers (see 'Defining a custom hunk-header' in |
223 | linkgit:gitattributes[5]). | |
2944e4e6 | 224 | |
317f63c2 RS |
225 | -<num>:: |
226 | -C <num>:: | |
227 | --context <num>:: | |
228 | Show <num> leading and trailing lines, and place a line | |
229 | containing `--` between contiguous groups of matches. | |
230 | ||
231 | -A <num>:: | |
232 | --after-context <num>:: | |
233 | Show <num> trailing lines, and place a line containing | |
234 | `--` between contiguous groups of matches. | |
235 | ||
236 | -B <num>:: | |
237 | --before-context <num>:: | |
238 | Show <num> leading lines, and place a line containing | |
239 | `--` between contiguous groups of matches. | |
240 | ||
ba8ea749 | 241 | -W:: |
317f63c2 | 242 | --function-context:: |
ba8ea749 RS |
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 | |
0cce88f1 PB |
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]). | |
ba8ea749 | 249 | |
68437ede CL |
250 | -m <num>:: |
251 | --max-count <num>:: | |
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 | |
256 | a non-zero status. | |
257 | ||
89f09dd3 | 258 | --threads <num>:: |
6e9ef296 DS |
259 | Number of `grep` worker threads to use. See 'NOTES ON THREADS' |
260 | and `grep.threads` in 'CONFIGURATION' for more information. | |
89f09dd3 | 261 | |
1c57119c JH |
262 | -f <file>:: |
263 | Read patterns from <file>, one per line. | |
25754125 ÆAB |
264 | + |
265 | Passing the pattern via <file> allows for providing a search pattern | |
266 | containing a \0. | |
267 | + | |
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. | |
272 | + | |
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`. | |
277 | + | |
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. | |
1c57119c | 281 | |
3acb27b6 ML |
282 | -e:: |
283 | The next parameter is the pattern. This option has to be | |
bfb8306d | 284 | used for patterns starting with `-` and should be used in |
fba0cbd9 ML |
285 | scripts passing user input to grep. Multiple patterns are |
286 | combined by 'or'. | |
287 | ||
3240240f SB |
288 | --and:: |
289 | --or:: | |
290 | --not:: | |
291 | ( ... ):: | |
23bfbb81 | 292 | Specify how multiple patterns are combined using Boolean |
fba0cbd9 ML |
293 | expressions. `--or` is the default operator. `--and` has |
294 | higher precedence than `--or`. `-e` has to be used for all | |
295 | patterns. | |
3acb27b6 | 296 | |
0ab7befa JH |
297 | --all-match:: |
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. | |
301 | ||
4ff61c21 JH |
302 | -q:: |
303 | --quiet:: | |
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. | |
306 | ||
bfb8306d | 307 | <tree>...:: |
2e48fcdb ML |
308 | Instead of searching tracked files in the working tree, search |
309 | blobs in the given trees. | |
1c57119c | 310 | |
fba0cbd9 | 311 | \--:: |
1c57119c | 312 | Signals the end of options; the rest of the parameters |
3ac44408 | 313 | are <pathspec> limiters. |
2aba319a | 314 | |
3ac44408 ML |
315 | <pathspec>...:: |
316 | If given, limit the search to paths matching at least one pattern. | |
317 | Both leading paths match and glob(7) patterns are supported. | |
93dbefb3 MR |
318 | + |
319 | For more details about the <pathspec> syntax, see the 'pathspec' entry | |
320 | in linkgit:gitglossary[7]. | |
2aba319a | 321 | |
76a8788c | 322 | EXAMPLES |
04416018 | 323 | -------- |
fba0cbd9 | 324 | |
6cf378f0 | 325 | `git grep 'time_t' -- '*.[ch]'`:: |
3ac44408 ML |
326 | Looks for `time_t` in all tracked .c and .h files in the working |
327 | directory and its subdirectories. | |
328 | ||
6cf378f0 | 329 | `git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)`:: |
fba0cbd9 ML |
330 | Looks for a line that has `#define` and either `MAX_PATH` or |
331 | `PATH_MAX`. | |
332 | ||
5d2fc913 | 333 | `git grep --all-match -e NODE -e Unexpected`:: |
0ab7befa JH |
334 | Looks for a line that has `NODE` or `Unexpected` in |
335 | files that have lines that match both. | |
336 | ||
93dbefb3 MR |
337 | `git grep solution -- :^Documentation`:: |
338 | Looks for `solution`, excluding files in `Documentation`. | |
339 | ||
1184a95e MT |
340 | NOTES ON THREADS |
341 | ---------------- | |
342 | ||
4a9357a1 | 343 | The `--threads` option (and the `grep.threads` configuration) will be ignored when |
1184a95e MT |
344 | `--open-files-in-pager` is used, forcing a single-threaded execution. |
345 | ||
346 | When grepping the object store (with `--cached` or giving tree objects), running | |
6e9ef296 DS |
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`. | |
1184a95e | 350 | |
414abf15 ÆAB |
351 | CONFIGURATION |
352 | ------------- | |
353 | ||
a2811dd7 | 354 | include::includes/cmd-config-section-all.txt[] |
414abf15 | 355 | |
a2811dd7 | 356 | include::config/grep.txt[] |
414abf15 | 357 | |
2aba319a JH |
358 | GIT |
359 | --- | |
9e1f0a85 | 360 | Part of the linkgit:git[1] suite |