git-grep โ Print lines matching a pattern
| Use Case | Command | Description |
|---|---|---|
| ๐ Search tracked files for a pattern | git grep <pattern> | Search working tree tracked files |
| ๐ค Caseโinsensitive search | git grep -i <pattern> | Ignore case when matching |
| ๐๏ธ Search only file names | git grep -l <pattern> | List files that contain matches |
| ๐ซ Invert match | git grep -v <pattern> | Show lines that do NOT match |
| ๐ข Show line numbers | git grep -n <pattern> | Prefix line numbers to output |
| ๐งฎ Count matches per file | git grep -c <pattern> | Show count of matching lines |
| ๐ Search staged (cached) content | git grep --cached <pattern> | Search blobs in the index |
| ๐ Search specific tree (commit/tag) | git grep <pattern> <tree> | Search blobs in given tree objects |
| ๐ Limit to file patterns | git grep <pattern> -- '*.c' | Restrict search to matching paths |
| ๐ฌ Boolean combinations | git grep -e 'pattern1' --and -e 'pattern2' | Combine patterns with AND/OR/NOT |
git grep [-a | --text] [-I] [--textconv] [-i | --ignore-case] [-w | --word-regexp]
[-v | --invert-match] [-h|-H] [--full-name]
[-E | --extended-regexp] [-G | --basic-regexp]
[-P | --perl-regexp]
[-F | --fixed-strings] [-n | --line-number] [--column]
[-l | --files-with-matches] [-L | --files-without-match]
[(-O | --open-files-in-pager) [<pager>]]
[-z | --null]
[ -o | --only-matching ] [-c | --count] [--all-match] [-q | --quiet]
[--max-depth <depth>] [--[no-]recursive]
[--color[=<when>] | --no-color]
[--break] [--heading] [-p | --show-function]
[-A <post-context>] [-B <pre-context>] [-C <context>]
[-W | --function-context]
[--threads <num>]
[-f <file>] [-e] <pattern>
[--and|--or|--not|(|)|-e <pattern>...]
[--recurse-submodules] [--parent-basename <basename>]
[ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
[--] [<pathspec>...]
Look for specified patterns in the tracked files in the work tree, blobs registered in the index file, or blobs in given tree objects. Patterns are lists of one or more search expressions separated by newline characters. An empty string as search expression matches all lines.
--cached ๐พ Instead of searching tracked files in the working tree, search blobs registered in the index file.--no-index ๐ Search files in the current directory that is not managed by Git.--untracked โ Also search in untracked files (in addition to tracked files).--no-exclude-standard ๐ซ Ignore .gitignore and also search in ignored files. Only useful with --untracked.--exclude-standard ๐ซ๐ Do not pay attention to ignored files specified via .gitignore. Only useful with --no-index.--recurse-submodules ๐ Recursively search in each active and checkedโout submodule. Has no effect if --no-index is given.-a, --text ๐ Process binary files as if they were text.--textconv ๐จ Honor textconv filter settings.--no-textconv ๐ซ๐จ Do not honor textconv filter settings (default).-i, --ignore-case ๐ค Ignore case differences between patterns and files.-I ๐ Donโt match the pattern in binary files.--max-depth <depth> ๐ Descend at most <depth> levels of directories for each <pathspec>. A value of -1 means no limit. Ignored if <pathspec> contains active wildcards.-r, --recursive ๐ Same as --max-depth=-1 (default).--no-recursive ๐โ Same as --max-depth=0.-w, --word-regexp ๐ค Match pattern only at word boundary (beginning/end of word).-v, --invert-match ๐ซ Select nonโmatching lines.-h, -H ๐ By default shows filename for each match. -h suppresses it; -H overrides an earlier -h.--full-name ๐ Output paths relative to project top directory instead of current subdirectory.-E, --extended-regexp, -G, --basic-regexp ๐ Use POSIX extended/basic regexp. Default is basic.-P, --perl-regexp ๐ช Use Perlโcompatible regular expressions. Optional compileโtime dependency.-F, --fixed-strings ๐ค Use fixed strings (no regex interpretation).-n, --line-number ๐ข Prefix line number to matching lines.--column ๐ Prefix 1โindexed byteโoffset of first match from start of line.-l, --files-with-matches, --name-only ๐ Show only file names that contain matches. -L, --files-without-match shows files that do NOT contain matches.-O[<pager>], --open-files-in-pager[=<pager>] ๐ Open matching files in pager (e.g., less, vi). If pager is unspecified, uses core.pager (see git-config(1)).-z, --null ๐ช Use \0 as pathname delimiter; unusual characters are printed verbatim (see core.quotePath in git-config(1)).-o, --only-matching ๐ฏ Print only the matched (nonโempty) parts of a line, each on a separate line.-c, --count ๐งฎ Show number of matching lines instead of each line.--color[=<when>] ๐ Show colored matches. Value: always (default), never, or auto.--no-color ๐ซ๐ Turn off match highlighting. Same as --color=never.--break ๐ฒ Print an empty line between matches from different files.--heading ๐ Show filename above matches instead of at start of each line.-p, --show-function ๐ Show preceding line containing function name of the match (same as git diff hunk headers).-<num>, -C <num>, --context <num> ๐ Show <num> leading and trailing lines, with -- between contiguous groups.-A <num>, --after-context <num> ๐ Show <num> trailing lines.-B <num>, --before-context <num> ๐ Show <num> leading lines.-W, --function-context ๐ Show surrounding text from previous function name to next, effectively showing the whole function containing the match.--threads <num> ๐งต Number of grep worker threads (see also grep.threads in CONFIGURATION).-f <file> ๐ Read patterns from file, one per line. Allows patterns containing \0 (depending on pattern type; PCRE v2 has widest support). Before Git 2.23.0 such patterns were silently treated as fixed.-e ๐ค The next parameter is the pattern. Must be used for patterns starting with -. Multiple -e patterns are combined with OR.--and, --or, --not, ( ... ) ๐ Specify Boolean combination of patterns. --or is default. --and has higher precedence than --or. Use -e for each pattern.--all-match โ
When multiple patterns are combined with --or, limit to files that have lines matching ALL of them.-q, --quiet ๐ Do not output matched lines; exit with status 0 when match found, nonโzero otherwise.<tree>... ๐ณ Search blobs in given tree objects instead of tracked files in working tree.-- ๐ง Signals end of options; remaining parameters are <pathspec> limiters.<pathspec>... ๐ If given, limit search to paths matching at least one pattern. Supports leading paths match and glob(7) patterns. See pathspec entry in gitglossary(7).git grep 'time_t' -- '*.[ch]'
๐ Looks for time_t in all tracked .c and .h files in the working directory and its subdirectories.
git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)
๐ Looks for a line that has #define and either MAX_PATH or PATH_MAX.
git grep --all-match -e NODE -e Unexpected
๐ Looks for a line that has NODE or Unexpected in files that have lines that match both.
git grep solution -- :^Documentation
๐ Looks for solution, excluding files in Documentation.
The --threads option (and grep.threads configuration) will be ignored when --open-files-in-pager is used, forcing singleโthreaded execution.
When grepping the object store (with --cached or giving tree objects), running with multiple threads might perform slower than single threaded if --textconv is given and there are many text conversions. So if you experience low performance in this case, it might be desirable to use --threads=1.
grep.lineNumber ๐ข If set to true, enable -n option by default.grep.column ๐ If set to true, enable the --column option by default.grep.patternType ๐ Set the default matching behavior: basic, extended, fixed, or perl enable the corresponding --*regexp/--fixed-strings option; default returns to default.grep.extendedRegexp ๐ If set to true, enable --extended-regexp by default. Ignored when grep.patternType is set to a nonโdefault value.grep.threads ๐งต Number of grep worker threads. If unset or 0, Git uses as many threads as logical cores available.grep.fullName ๐ If set to true, enable --full-name by default.grep.fallbackToNoIndex ๐ If set to true, fall back to git grep --no-index if executed outside a Git repository. Defaults to false.Part of the git(1) suite. Git 2.34.1 02/26/2026
Generated by phpman v4.9.25-25-g40dbf62 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-14 07:15 @216.73.217.135
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-pro / taotoken.net / www.chedong.com - original format