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