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
30
32 Look for specified patterns in the tracked files in the work tree,
33 blobs registered in the index file, or blobs in given tree objects.
34 Patterns are lists of one or more search expressions separated by
35 newline characters. An empty string as search expression matches all
36 lines.
37
39 grep.lineNumber
40 If set to true, enable -n option by default.
41
42 grep.column
43 If set to true, enable the --column option by default.
44
45 grep.patternType
46 Set the default matching behavior. Using a value of basic,
47 extended, fixed, or perl will enable the --basic-regexp,
48 --extended-regexp, --fixed-strings, or --perl-regexp option
49 accordingly, while the value default will return to the default
50 matching behavior.
51
52 grep.extendedRegexp
53 If set to true, enable --extended-regexp option by default. This
54 option is ignored when the grep.patternType option is set to a
55 value other than default.
56
57 grep.threads
58 Number of grep worker threads to use. If unset (or set to 0), 8
59 threads are used by default (for now).
60
61 grep.fullName
62 If set to true, enable --full-name option by default.
63
64 grep.fallbackToNoIndex
65 If set to true, fall back to git grep --no-index if git grep is
66 executed outside of a git repository. Defaults to false.
67
69 --cached
70 Instead of searching tracked files in the working tree, search
71 blobs registered in the index file.
72
73 --no-index
74 Search files in the current directory that is not managed by Git.
75
76 --untracked
77 In addition to searching in the tracked files in the working tree,
78 search also in untracked files.
79
80 --no-exclude-standard
81 Also search in ignored files by not honoring the .gitignore
82 mechanism. Only useful with --untracked.
83
84 --exclude-standard
85 Do not pay attention to ignored files specified via the .gitignore
86 mechanism. Only useful when searching files in the current
87 directory with --no-index.
88
89 --recurse-submodules
90 Recursively search in each submodule that has been initialized and
91 checked out in the repository. When used in combination with the
92 <tree> option the prefix of all submodule output will be the name
93 of the parent project’s <tree> object.
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 Output \0 instead of the character that normally follows a file
180 name.
181
182 -o, --only-matching
183 Print only the matched (non-empty) parts of a matching line, with
184 each such part on a separate output line.
185
186 -c, --count
187 Instead of showing every matched line, show the number of lines
188 that match.
189
190 --color[=<when>]
191 Show colored matches. The value must be always (the default),
192 never, or auto.
193
194 --no-color
195 Turn off match highlighting, even when the configuration file gives
196 the default to color output. Same as --color=never.
197
198 --break
199 Print an empty line between matches from different files.
200
201 --heading
202 Show the filename above the matches in that file instead of at the
203 start of each shown line.
204
205 -p, --show-function
206 Show the preceding line that contains the function name of the
207 match, unless the matching line is a function name itself. The name
208 is determined in the same way as git diff works out patch hunk
209 headers (see Defining a custom hunk-header in gitattributes(5)).
210
211 -<num>, -C <num>, --context <num>
212 Show <num> leading and trailing lines, and place a line containing
213 -- between contiguous groups of matches.
214
215 -A <num>, --after-context <num>
216 Show <num> trailing lines, and place a line containing -- between
217 contiguous groups of matches.
218
219 -B <num>, --before-context <num>
220 Show <num> leading lines, and place a line containing -- between
221 contiguous groups of matches.
222
223 -W, --function-context
224 Show the surrounding text from the previous line containing a
225 function name up to the one before the next function name,
226 effectively showing the whole function in which the match was
227 found.
228
229 --threads <num>
230 Number of grep worker threads to use. See grep.threads in
231 CONFIGURATION for more information.
232
233 -f <file>
234 Read patterns from <file>, one per line.
235
236 -e
237 The next parameter is the pattern. This option has to be used for
238 patterns starting with - and should be used in scripts passing user
239 input to grep. Multiple patterns are combined by or.
240
241 --and, --or, --not, ( ... )
242 Specify how multiple patterns are combined using Boolean
243 expressions. --or is the default operator. --and has higher
244 precedence than --or. -e has to be used for all patterns.
245
246 --all-match
247 When giving multiple pattern expressions combined with --or, this
248 flag is specified to limit the match to files that have lines to
249 match all of them.
250
251 -q, --quiet
252 Do not output matched lines; instead, exit with status 0 when there
253 is a match and with non-zero status when there isn’t.
254
255 <tree>...
256 Instead of searching tracked files in the working tree, search
257 blobs in the given trees.
258
259 --
260 Signals the end of options; the rest of the parameters are
261 <pathspec> limiters.
262
263 <pathspec>...
264 If given, limit the search to paths matching at least one pattern.
265 Both leading paths match and glob(7) patterns are supported.
266
267 For more details about the <pathspec> syntax, see the pathspec
268 entry in gitglossary(7).
269
271 git grep 'time_t' -- '*.[ch]'
272 Looks for time_t in all tracked .c and .h files in the working
273 directory and its subdirectories.
274
275 git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)
276 Looks for a line that has #define and either MAX_PATH or PATH_MAX.
277
278 git grep --all-match -e NODE -e Unexpected
279 Looks for a line that has NODE or Unexpected in files that have
280 lines that match both.
281
282 git grep solution -- :^Documentation
283 Looks for solution, excluding files in Documentation.
284
286 Part of the git(1) suite
287
288
289
290Git 2.21.0 02/24/2019 GIT-GREP(1)