Git
日本語 ▾Topics ▾Latest version ▾ git-grep last updated in 2.45.0

NAME

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) [<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]
	   [(-m | --max-count) <num>]
	   [--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>…​]

説明

作業ツリー内の追跡されたファイルやインデックスファイルに登録された Blob、または指定されたツリーオブジェクト内の Blob で指定されたパターンを検索します。パターンは改行文字で区切られた 1つ以上の検索式のリストです。検索式として空の文字列を指定するとすべての行に一致します。

オプション

--cached

作業ツリー内の追跡されたファイルを検索する代わりに、インデックスファイルに登録されている Blob を検索します。

--no-index

Git によって管理されていない現在のディレクトリ内のファイルを検索します。

--untracked

作業ツリー内の追跡されたファイルだけでなく、追跡されていないファイルも検索します。

--no-exclude-standard

.gitignore メカニズムを無視して、無視されたファイル内も検索します。--untracked との併用で役立ちます。

--exclude-standard

.gitignore メカニズムで指定された無視されたファイルには注意を払いません。--no-index を使用して現在のディレクトリ内のファイルを検索する場合に役立ちます。

--recurse-submodules

リポジトリ内でアクティブでチェックアウトされている各サブモジュールを再帰的に検索します。<tree> オプションと組み合わせて使用すると、すべてのサブモジュール出力のプレフィックスは親プロジェクトの <tree> オブジェクトの名前になります。--no-index が指定されている場合、このオプションは効果がありません。

-a
--text

バイナリファイルをテキストのように処理します。

--textconv

textconv フィルター設定を使用します。

--no-textconv

textconv フィルター設定を使用しません。これがデフォルトです。

-i
--ignore-case

パターンとファイルの大文字と小文字の違いを無視します。

-I

バイナリファイル内のパターンを一致させない。

--max-depth <depth>

コマンドラインで指定された各 <pathspec> について、最大 <depth> レベルのディレクトリまで降りていきます。値 -1 は制限がないことを意味します。このオプションは、<pathspec> にアクティブなワイルドカードが含まれている場合は無視されます。つまり、"a*" が "a*" という名前のディレクトリと一致する場合、"*" は文字通り一致するため、--max-depth は引き続き有効です。

-r
--recursive

--max-depth=-1 と同じです。これがデフォルトです。

--no-recursive

--max-depth=0 と同じです。

-w
--word-regexp

パターンは単語境界でのみ一致します (行の先頭で始まるか、単語以外の文字が先行するか、行の末尾で終わるか、単語以外の文字が後に続きます)。

-v
--invert-match

一致しない行を選択します。

-h
-H

デフォルトでは、コマンドは一致する各ファイル名を表示します。-h オプションは、この出力を抑制するために使用されます。-H は完全性のために存在し、コマンドラインで先に指定された -h を上書きする以外は何も行いません。

--full-name

サブディレクトリから実行すると、コマンドは通常、現在のディレクトリを基準としたパスを出力します。このオプションは、パスがプロジェクトのトップディレクトリを基準として出力されるように強制します。

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

パターンには POSIX 拡張/基本正規表現を使用します。デフォルトでは基本正規表現が使用されます。

-P
--perl-regexp

パターンには Perl 互換の正規表現を使用します。

Support for these types of regular expressions is an optional compile-time dependency. If Git wasn’t compiled with support for them providing this option will cause it to die.

-F
--fixed-strings

パターンには固定文字列を使用します (パターンを正規表現として解釈しません)。

-n
--line-number

一致する行の前に行番号を付けます。

--column

一致する行の先頭から最初の一致の 1 インデックス バイト オフセットをプレフィックスとして追加します。

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

一致した行をすべて表示するのではなく、一致した行を含む (あるいは含まない) ファイル名のみを表示します。 git diff との互換性を高めるために、--name-only--files-with-matches の同義語です。

-O[<pager>]
--open-files-in-pager[=<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

Use \0 as the delimiter for pathnames in the output, and print them verbatim. Without this option, pathnames with "unusual" characters are quoted as explained for the configuration variable core.quotePath (see git-config[1]).

-o
--only-matching

一致する行の一致した部分 (空でない部分) のみを、それぞれ別々の行に出力します。

-c
--count

一致した行をすべて表示するのではなく、一致する行の数を表示します。

--color[=<いつ>]

一致内容を色付きで表示します。値は always (デフォルト)、never、または auto のいずれかである必要があります。

--no-color

設定ファイルでデフォルトでカラー出力が指定されている場合でも、一致の強調表示をオフにします。--color=never と同じです。

--break

異なるファイルからの一致の間に空行を出力します。

--heading

表示される各行の先頭ではなく、そのファイル内の一致の上にファイル名を表示します。

-p
--show-function

一致する行が関数名自体でない限り、一致する関数名を含む前の行を表示します。名前は、git diff がパッチ ハンク ヘッダーを処理するのと同じ方法で決定されます (gitattributes[5] の「カスタム ハンク ヘッダーの定義」を参照)。

-<num>
-C <num>
--context <num>

<num> 行の先頭と末尾を表示し、連続する一致グループの間に -- を含む行を配置します。

-A <num>
--after-context <num>

<num> 行末を表示し、連続する一致グループの間に -- を含む行を配置します。

-B <num>
--before-context <num>

<num> 行の先頭を表示し、連続する一致グループの間に -- を含む行を配置します。

-W
--function-context

関数名を含む前の行から次の関数名の前までの周囲のテキストを表示し、一致が見つかった関数全体を効果的に表示します。関数名は、git diff がパッチ ハンク ヘッダーを処理するのと同じ方法で決定されます (gitattributes[5] の「カスタム ハンク ヘッダーの定義」を参照)。

-m <数>
--max-count <num>

ファイルごとの一致件数を制限します。-v または --invert-match オプションを使用すると、指定した件数の不一致が検出されると検索が停止します。値 -1 を指定すると、無制限の結果が返されます (デフォルト)。値 0 を指定すると、ゼロ以外のステータスで直ちに終了します。

--threads <num>

使用する grep ワーカー スレッドの数。詳細については、CONFIGURATIONgrep.threads を参照してください。

-f <file>

<file> からパターンを 1 行に 1 つずつ読み取ります。

Passing the pattern via <file> allows for providing a search pattern containing a \0.

Not all pattern types support patterns containing \0. Git will error out if a given pattern type can’t support such a pattern. The --perl-regexp pattern type when compiled against the PCRE v2 backend has the widest support for these types of patterns.

In versions of Git before 2.23.0 patterns containing \0 would be silently considered fixed. This was never documented, there were also odd and undocumented interactions between e.g. non-ASCII patterns containing \0 and --ignore-case.

In future versions we may learn to support patterns containing \0 for more search backends, until then we’ll die when the pattern type in question doesn’t support them.

-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

When giving multiple pattern expressions combined with --or, this flag is specified to limit the match to files that have lines to match all of them.

-q
--quiet

Do not output matched lines; instead, exit with status 0 when there is a match and with non-zero status when there isn’t.

<tree>…​

Instead of searching tracked files in the working tree, search blobs in the given trees.

--

Signals the end of options; the rest of the parameters are <pathspec> limiters.

<パススペック>…​

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]'

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.

NOTES ON THREADS

The --threads option (and the grep.threads configuration) will be ignored when --open-files-in-pager is used, forcing a 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 too many text conversions. So if you experience low performance in this case, it might be desirable to use --threads=1.

設定

Warning

Missing ja/includes/cmd-config-section-all.txt

See original version for this content.

Warning

Missing ja/config/grep.txt

See original version for this content.

GIT

Part of the git[1] suite

scroll-to-top