Git
简体中文 ▾ Topics ▾ Latest version ▾ git-grep last updated in 2.44.0

名称

git-grep - 打印与模式匹配的行

概述

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) [<分页器>]]
	   [-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 <后上下文>] [-B <前上下文>] [-C <上下文>]
	   [-W | --function-context]
	   [(-m | --max-count) <数量>]
	   [--threads <数量>]
	   [-f <file>] [-e] <模式>
	   [--and|--or|--not|(|)|-e <模式>…​]
	   [--recurse-submodules] [--parent-basename <basename>]
	   [ [--[no-]exclude-standard] [--cached | --no-index | --untracked] | <目录树>…​]
	   [--] [<路径规范>…​]

描述

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

与其搜索工作区中的跟踪文件,不如搜索索引文件中注册的 blobs。

--no-index

搜索当前目录中不被 Git 管理的文件。

--untracked

除了在工作区中的跟踪文件中搜索外,还可以在未跟踪的文件中搜索。

--no-exclude-standard

也可以通过不遵守 .gitignore 机制在被忽略的文件中搜索。只对 --untracked 有用。

--exclude-standard

Do not pay attention to ignored files specified via the .gitignore mechanism. Only useful when searching files in the current directory with --no-index.

--recurse-submodules

Recursively search in each submodule that is active and checked out in the repository. When used in combination with the <tree> option the prefix of all submodule output will be the name of the parent project’s <tree> object. This option has no effect if --no-index is given.

-a
--text

像处理文本一样处理二进制文件。

--textconv

认可 textconv 过滤器设置。

--no-textconv

Do not honor textconv filter settings. This is the default.

-i
--ignore-case

忽略图案和文件之间的大小写差异。

-I

不匹配二进制文件中的模式。

--max-depth <深度>

For each <pathspec> given on command line, descend at most <depth> levels of directories. A value of -1 means no limit. This option is ignored if <pathspec> contains active wildcards. In other words if "a*" matches a directory named "a*", "*" is matched literally so --max-depth is still effective.

-r
--recursive

--max-depth=-1 相同;这是默认值。

--no-recursive

--max-depth=0 相同。

-w
--word-regexp

只在词的边界处匹配模式(要么从一行的开头开始,要么前面是一个非词的字符;要么在一行的结尾结束,要么后面是一个非词的字符)。

-v
--invert-match

选择非匹配的行。

-h
-H

By default, the command shows the filename for each match. -h option is used to suppress this output. -H is there for completeness and does not do anything except it overrides -h given earlier on the command line.

--full-name

When run from a subdirectory, the command usually outputs paths relative to the current directory. This option forces paths to be output relative to the project top directory.

-E
--extended-regexp
-G
--basic-regexp

Use POSIX extended/basic regexp for patterns. Default is to use basic regexp.

-P
--perl-regexp

使用与 Perl 兼容的正则表达式的模式。

对这些类型的正则表达式的支持是一个可选的编译时依赖。如果Git在编译时没有对它们的支持,提供这个选项将导致它死亡。

-F
--fixed-strings

使用固定的字符串来表示模式(不要把模式解释为一个重合词)。

-n
--line-number

在匹配的行前加上行号。

--column

前缀为从匹配行开始的第一个匹配的索引的字节偏移量。

-l
--files-with-matches
--name-only
-L
--files-without-match

Instead of showing every matched line, show only the names of files that contain (or do not contain) matches. For better compatibility with git diff, --name-only is a synonym for --files-with-matches.

-O[<分页器>]
--open-files-in-pager[=<分页器>]

Open the matching files in the pager (not the output of grep). If the pager happens to be "less" or "vi", and the user specified only one pattern, the first file is positioned at the first match automatically. The pager argument is optional; if specified, it must be stuck to the option without a space. If pager is unspecified, the default pager will be used (see core.pager in git-config[1]).

-z
--null

使用\0作为输出中路径名的分隔符,并逐字打印它们。按照配置变量core.quotePath的说明,引用带有“ unusual”字符的路径名 (查阅git-config[1])。

-o
--only-matching

只打印匹配行中的匹配(非空)部分,每一个这样的部分都在单独的输出行中。

-c
--count

与其显示每一个匹配的行,不如显示匹配的行数。

--color[=<when>]

Show colored matches. The value must be always (the default), never, or auto.

--no-color

Turn off match highlighting, even when the configuration file gives the default to color output. Same as --color=never.

--break

在不同文件的匹配之间打印一个空行。

--heading

在该文件中的匹配项上方显示文件名,而不是在每个显示行的开头。

-p
--show-function

Show the preceding line that contains the function name of the match, unless the matching line is a function name itself. The name is determined in the same way as git diff works out patch hunk headers (see Defining a custom hunk-header in gitattributes[5]).

-<num>
-C <数量>
--context <数量>

显示 <num> 前导行和后导行,并在连续的匹配组之间放置一个包含`--`的行。

-A <num>
--after-context <数量>

显示 <num> 尾行,并在连续的匹配组之间放置一个包含 -- 的行。

-B <num>
--before-context <数量>

显示 <num> 引导行,并在连续的匹配组之间放置一个包含 -- 的行。

-W
--function-context

显示从包含函数名的前一行到下一个函数名之前的周围文本,有效地显示找到匹配的整个函数。函数名的确定方式与 git diff 确定补丁组头的方式相同(见 gitattributes[5] 中的 ‘定义自定义组头’)。

-C <数量>
--max-count <数量>

限制每个文件的匹配数量。当使用 -v--invert-match 选项时,搜索会在指定的非匹配数量后停止。值为 -1 将返回无限的结果(默认)。值为 0 将立即退出,状态为非零。

--threads <数量>

Number of grep worker threads to use. See grep.threads in CONFIGURATION for more information.

-f <文件>

从 <文件> 中读取模式,每行一个。

通过<文件> 传递模式,可以提供一个包含一个 \0 的搜索模式。

不是所有的模式类型都支持包含 \0 的模式。如果一个给定的模式类型不支持这样的模式,Git 会出错。--perl-regexp 模式类型在针对 PCRE v2 后端进行编译时,对这些类型的模式有最广泛的支持。

在 2.23.0 之前的 Git 版本中,含有 \0 的模式会被默默地认为是固定的。这一点从未被记录下来,还有一些奇怪的、未被记录的互动,例如包含 \0 的非 ASCII 模式和 --ignore-case 之间。

在未来的版本中,我们可能会学会为更多的搜索后端支持含有 \0 的模式,在此之前,当有关的模式类型不支持它们时,我们就会死心。

-e

The next parameter is the pattern. This option has to be used for patterns starting with - and should be used in scripts passing user input to grep. Multiple patterns are combined by or.

--and
--or
--not
( …​ )

Specify how multiple patterns are combined using Boolean expressions. --or is the default operator. --and has higher precedence than --or. -e has to be used for all patterns.

--all-match

当给出多个模式表达式与 --or 组合时,指定这个标志是为了将匹配限制在有行的文件中,以匹配所有的文件。

-q
--quiet

不输出匹配的行;相反,当有匹配时以状态 0 退出,没有匹配时以非零状态退出。

<目录树>…​

与其在工作区中搜索被追踪的文件,不如在给定的目录树中搜索 blobs。

--

标志着选项的结束;其余的参数是 <路径规范> 限制器。

<pathspec>…​

If given, limit the search to paths matching at least one pattern. Both leading paths match and glob(7) patterns are supported.

有关 <指定路径> 格式的更多细节,请参考 gitglossary[7]

实例

git grep 'time_t' -- '*.[ch]'

在工作目录及其子目录中所有跟踪的 .c 和 .h 文件中寻找 time_t

git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)

寻找有 #defineMAX_PATHPATH_MAX 的行。

git grep --all-match -e NODE -e Unexpected

在文件中寻找有 NODEUnexpected 字样的行,这些行与这两个字相匹配。

git grep solution -- :^Documentation

寻找 solution,排除 `Documentation`中的文件。

线条的注释

当使用 --open-files-in-pager 时,--threads 选项(和 grep.threads 配置)将被忽略,强制单线程执行。

当检索对象存储时(使用 --cached 或给出树状对象),如果给出 --textconv 并且有太多的文本转换,多线程运行可能比单线程运行要慢。因此,如果你在这种情况下遇到低性能,使用 --threads=1 可能是可取的。

配置

Warning

Missing zh_HANS-CN/includes/cmd-config-section-all.txt

See original version for this content.

Warning

Missing zh_HANS-CN/config/grep.txt

See original version for this content.

GIT

属于 git[1] 文档

scroll-to-top