]>
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] |
d2355d76 | 16 | [-F | --fixed-strings] [-n | --line-number] |
cf0d720b | 17 | [-l | --files-with-matches] [-L | --files-without-match] |
0af88c15 | 18 | [(-O | --open-files-in-pager) [<pager>]] |
83caecca | 19 | [-z | --null] |
4ff61c21 | 20 | [-c | --count] [--all-match] [-q | --quiet] |
a91f453f | 21 | [--max-depth <depth>] |
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] |
89f09dd3 | 26 | [--threads <num>] |
cf0d720b | 27 | [-f <file>] [-e] <pattern> |
ec2537be | 28 | [--and|--or|--not|(|)|-e <pattern>...] |
0460ed2c | 29 | [ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...] |
3ac44408 | 30 | [--] [<pathspec>...] |
2aba319a JH |
31 | |
32 | DESCRIPTION | |
33 | ----------- | |
3ac44408 | 34 | Look for specified patterns in the tracked files in the work tree, blobs |
526a858a RS |
35 | registered in the index file, or blobs in given tree objects. Patterns |
36 | are lists of one or more search expressions separated by newline | |
37 | characters. An empty string as search expression matches all lines. | |
2aba319a JH |
38 | |
39 | ||
b22520a3 JR |
40 | CONFIGURATION |
41 | ------------- | |
42 | ||
43 | grep.lineNumber:: | |
23f8239b | 44 | If set to true, enable `-n` option by default. |
b22520a3 | 45 | |
84befcd0 S |
46 | grep.patternType:: |
47 | Set the default matching behavior. Using a value of 'basic', 'extended', | |
48 | 'fixed', or 'perl' will enable the '--basic-regexp', '--extended-regexp', | |
49 | '--fixed-strings', or '--perl-regexp' option accordingly, while the | |
50 | value 'default' will return to the default matching behavior. | |
51 | ||
b22520a3 | 52 | grep.extendedRegexp:: |
84befcd0 | 53 | If set to true, enable '--extended-regexp' option by default. This |
ae9f6311 | 54 | option is ignored when the `grep.patternType` option is set to a value |
84befcd0 | 55 | other than 'default'. |
b22520a3 | 56 | |
89f09dd3 VL |
57 | grep.threads:: |
58 | Number of grep worker threads to use. If unset (or set to 0), | |
59 | 8 threads are used by default (for now). | |
60 | ||
6453f7b3 AS |
61 | grep.fullName:: |
62 | If set to true, enable '--full-name' option by default. | |
63 | ||
ecd9ba61 TG |
64 | grep.fallbackToNoIndex:: |
65 | If set to true, fall back to git grep --no-index if git grep | |
66 | is executed outside of a git repository. Defaults to false. | |
67 | ||
b22520a3 | 68 | |
2aba319a JH |
69 | OPTIONS |
70 | ------- | |
1c57119c | 71 | --cached:: |
2e48fcdb ML |
72 | Instead of searching tracked files in the working tree, search |
73 | blobs registered in the index file. | |
74 | ||
75 | --no-index:: | |
2de9b711 | 76 | Search files in the current directory that is not managed by Git. |
1c57119c | 77 | |
0a93fb8a JH |
78 | --untracked:: |
79 | In addition to searching in the tracked files in the working | |
80 | tree, search also in untracked files. | |
81 | ||
82 | --no-exclude-standard:: | |
83 | Also search in ignored files by not honoring the `.gitignore` | |
84 | mechanism. Only useful with `--untracked`. | |
85 | ||
86 | --exclude-standard:: | |
87 | Do not pay attention to ignored files specified via the `.gitignore` | |
88 | mechanism. Only useful when searching files in the current directory | |
89 | with `--no-index`. | |
1c57119c | 90 | |
3240240f SB |
91 | -a:: |
92 | --text:: | |
1c57119c JH |
93 | Process binary files as if they were text. |
94 | ||
335ec3bf JK |
95 | --textconv:: |
96 | Honor textconv filter settings. | |
97 | ||
98 | --no-textconv:: | |
99 | Do not honor textconv filter settings. | |
100 | This is the default. | |
101 | ||
3240240f SB |
102 | -i:: |
103 | --ignore-case:: | |
1c57119c JH |
104 | Ignore case differences between the patterns and the |
105 | files. | |
106 | ||
cf0d720b AR |
107 | -I:: |
108 | Don't match the pattern in binary files. | |
109 | ||
a91f453f | 110 | --max-depth <depth>:: |
3ac44408 | 111 | For each <pathspec> given on command line, descend at most <depth> |
a91f453f | 112 | levels of directories. A negative value means no limit. |
8c69c1f9 NTND |
113 | This option is ignored if <pathspec> contains active wildcards. |
114 | In other words if "a*" matches a directory named "a*", | |
115 | "*" is matched literally so --max-depth is still effective. | |
a91f453f | 116 | |
3240240f SB |
117 | -w:: |
118 | --word-regexp:: | |
1c57119c JH |
119 | Match the pattern only at word boundary (either begin at the |
120 | beginning of a line, or preceded by a non-word character; end at | |
121 | the end of a line or followed by a non-word character). | |
122 | ||
3240240f SB |
123 | -v:: |
124 | --invert-match:: | |
1c57119c JH |
125 | Select non-matching lines. |
126 | ||
3240240f SB |
127 | -h:: |
128 | -H:: | |
17fd965d JH |
129 | By default, the command shows the filename for each |
130 | match. `-h` option is used to suppress this output. | |
131 | `-H` is there for completeness and does not do anything | |
132 | except it overrides `-h` given earlier on the command | |
133 | line. | |
134 | ||
0d042fec JH |
135 | --full-name:: |
136 | When run from a subdirectory, the command usually | |
137 | outputs paths relative to the current directory. This | |
138 | option forces paths to be output relative to the project | |
139 | top directory. | |
140 | ||
3240240f SB |
141 | -E:: |
142 | --extended-regexp:: | |
143 | -G:: | |
144 | --basic-regexp:: | |
1c57119c JH |
145 | Use POSIX extended/basic regexp for patterns. Default |
146 | is to use basic regexp. | |
5b2bcc7b | 147 | |
63e7e9d8 MK |
148 | -P:: |
149 | --perl-regexp:: | |
150 | Use Perl-compatible regexp for patterns. Requires libpcre to be | |
151 | compiled in. | |
152 | ||
3240240f SB |
153 | -F:: |
154 | --fixed-strings:: | |
cf0d720b AR |
155 | Use fixed strings for patterns (don't interpret pattern |
156 | as a regex). | |
157 | ||
1c57119c | 158 | -n:: |
7d6cb10b | 159 | --line-number:: |
1c57119c | 160 | Prefix the line number to matching lines. |
2aba319a | 161 | |
3240240f SB |
162 | -l:: |
163 | --files-with-matches:: | |
164 | --name-only:: | |
165 | -L:: | |
166 | --files-without-match:: | |
1c57119c JH |
167 | Instead of showing every matched line, show only the |
168 | names of files that contain (or do not contain) matches. | |
bfb8306d ML |
169 | For better compatibility with 'git diff', `--name-only` is a |
170 | synonym for `--files-with-matches`. | |
2aba319a | 171 | |
318ca615 MM |
172 | -O[<pager>]:: |
173 | --open-files-in-pager[=<pager>]:: | |
678e484b JS |
174 | Open the matching files in the pager (not the output of 'grep'). |
175 | If the pager happens to be "less" or "vi", and the user | |
176 | specified only one pattern, the first file is positioned at | |
2b594bf9 MM |
177 | the first match automatically. The `pager` argument is |
178 | optional; if specified, it must be stuck to the option | |
179 | without a space. If `pager` is unspecified, the default pager | |
180 | will be used (see `core.pager` in linkgit:git-config[1]). | |
678e484b | 181 | |
83caecca RZ |
182 | -z:: |
183 | --null:: | |
184 | Output \0 instead of the character that normally follows a | |
185 | file name. | |
186 | ||
3240240f SB |
187 | -c:: |
188 | --count:: | |
1c57119c JH |
189 | Instead of showing every matched line, show the number of |
190 | lines that match. | |
191 | ||
73e9da01 | 192 | --color[=<when>]:: |
7e8f59d5 | 193 | Show colored matches. |
73e9da01 | 194 | The value must be always (the default), never, or auto. |
7e8f59d5 RS |
195 | |
196 | --no-color:: | |
197 | Turn off match highlighting, even when the configuration file | |
198 | gives the default to color output. | |
73e9da01 | 199 | Same as `--color=never`. |
7e8f59d5 | 200 | |
a8f0e764 RS |
201 | --break:: |
202 | Print an empty line between matches from different files. | |
203 | ||
1d84f72e RS |
204 | --heading:: |
205 | Show the filename above the matches in that file instead of | |
206 | at the start of each shown line. | |
207 | ||
2944e4e6 RS |
208 | -p:: |
209 | --show-function:: | |
210 | Show the preceding line that contains the function name of | |
211 | the match, unless the matching line is a function name itself. | |
60ecac98 RS |
212 | The name is determined in the same way as 'git diff' works out |
213 | patch hunk headers (see 'Defining a custom hunk-header' in | |
214 | linkgit:gitattributes[5]). | |
2944e4e6 | 215 | |
317f63c2 RS |
216 | -<num>:: |
217 | -C <num>:: | |
218 | --context <num>:: | |
219 | Show <num> leading and trailing lines, and place a line | |
220 | containing `--` between contiguous groups of matches. | |
221 | ||
222 | -A <num>:: | |
223 | --after-context <num>:: | |
224 | Show <num> trailing lines, and place a line containing | |
225 | `--` between contiguous groups of matches. | |
226 | ||
227 | -B <num>:: | |
228 | --before-context <num>:: | |
229 | Show <num> leading lines, and place a line containing | |
230 | `--` between contiguous groups of matches. | |
231 | ||
ba8ea749 | 232 | -W:: |
317f63c2 | 233 | --function-context:: |
ba8ea749 RS |
234 | Show the surrounding text from the previous line containing a |
235 | function name up to the one before the next function name, | |
236 | effectively showing the whole function in which the match was | |
237 | found. | |
238 | ||
89f09dd3 VL |
239 | --threads <num>:: |
240 | Number of grep worker threads to use. | |
241 | See `grep.threads` in 'CONFIGURATION' for more information. | |
242 | ||
1c57119c JH |
243 | -f <file>:: |
244 | Read patterns from <file>, one per line. | |
245 | ||
3acb27b6 ML |
246 | -e:: |
247 | The next parameter is the pattern. This option has to be | |
bfb8306d | 248 | used for patterns starting with `-` and should be used in |
fba0cbd9 ML |
249 | scripts passing user input to grep. Multiple patterns are |
250 | combined by 'or'. | |
251 | ||
3240240f SB |
252 | --and:: |
253 | --or:: | |
254 | --not:: | |
255 | ( ... ):: | |
23bfbb81 | 256 | Specify how multiple patterns are combined using Boolean |
fba0cbd9 ML |
257 | expressions. `--or` is the default operator. `--and` has |
258 | higher precedence than `--or`. `-e` has to be used for all | |
259 | patterns. | |
3acb27b6 | 260 | |
0ab7befa JH |
261 | --all-match:: |
262 | When giving multiple pattern expressions combined with `--or`, | |
263 | this flag is specified to limit the match to files that | |
264 | have lines to match all of them. | |
265 | ||
4ff61c21 JH |
266 | -q:: |
267 | --quiet:: | |
268 | Do not output matched lines; instead, exit with status 0 when | |
269 | there is a match and with non-zero status when there isn't. | |
270 | ||
bfb8306d | 271 | <tree>...:: |
2e48fcdb ML |
272 | Instead of searching tracked files in the working tree, search |
273 | blobs in the given trees. | |
1c57119c | 274 | |
fba0cbd9 | 275 | \--:: |
1c57119c | 276 | Signals the end of options; the rest of the parameters |
3ac44408 | 277 | are <pathspec> limiters. |
2aba319a | 278 | |
3ac44408 ML |
279 | <pathspec>...:: |
280 | If given, limit the search to paths matching at least one pattern. | |
281 | Both leading paths match and glob(7) patterns are supported. | |
2aba319a | 282 | |
04416018 ML |
283 | Examples |
284 | -------- | |
fba0cbd9 | 285 | |
6cf378f0 | 286 | `git grep 'time_t' -- '*.[ch]'`:: |
3ac44408 ML |
287 | Looks for `time_t` in all tracked .c and .h files in the working |
288 | directory and its subdirectories. | |
289 | ||
6cf378f0 | 290 | `git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)`:: |
fba0cbd9 ML |
291 | Looks for a line that has `#define` and either `MAX_PATH` or |
292 | `PATH_MAX`. | |
293 | ||
5d2fc913 | 294 | `git grep --all-match -e NODE -e Unexpected`:: |
0ab7befa JH |
295 | Looks for a line that has `NODE` or `Unexpected` in |
296 | files that have lines that match both. | |
297 | ||
2aba319a JH |
298 | GIT |
299 | --- | |
9e1f0a85 | 300 | Part of the linkgit:git[1] suite |