<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect2 SYSTEM "../../../dtd/dblite.dtd">

<sect2 lang="en" id="git-grep(1)">
    <title>git-grep(1)</title>
    <date>2024-07-02</date>
<revhistory><revision><date>2024-07-02</date></revision></revhistory>
<indexterm>
<primary>git-grep(1)</primary>
</indexterm>
<simplesect id="git-grep(1)__name">
<title>NAME</title>
<simpara>git-grep - Print lines matching a pattern</simpara>
</simplesect>
<simplesect id="git-grep(1)__synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git grep</emphasis> [-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) [&lt;pager&gt;]]
           [-z | --null]
           [ -o | --only-matching ] [-c | --count] [--all-match] [-q | --quiet]
           [--max-depth &lt;depth&gt;] [--[no-]recursive]
           [--color[=&lt;when&gt;] | --no-color]
           [--break] [--heading] [-p | --show-function]
           [-A &lt;post-context&gt;] [-B &lt;pre-context&gt;] [-C &lt;context&gt;]
           [-W | --function-context]
           [(-m | --max-count) &lt;num&gt;]
           [--threads &lt;num&gt;]
           [-f &lt;file&gt;] [-e] &lt;pattern&gt;
           [--and|--or|--not|(|)|-e &lt;pattern&gt;&#8230;]
           [--recurse-submodules] [--parent-basename &lt;basename&gt;]
           [ [--[no-]exclude-standard] [--cached | --untracked | --no-index] | &lt;tree&gt;&#8230;]
           [--] [&lt;pathspec&gt;&#8230;]</literallayout>
</blockquote>
</simplesect>
<simplesect id="git-grep(1)__description">
<title>DESCRIPTION</title>
<simpara>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.</simpara>
</simplesect>
<simplesect id="git-grep(1)__options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
--cached
</term>
<listitem>
<simpara>
        Instead of searching tracked files in the working tree, search
        blobs registered in the index file.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--untracked
</term>
<listitem>
<simpara>
        In addition to searching in the tracked files in the working
        tree, search also in untracked files.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--no-index
</term>
<listitem>
<simpara>
        Search files in the current directory that is not managed by Git,
        or by ignoring that the current directory is managed by Git.  This
        is rather similar to running the regular <emphasis>grep(1)</emphasis> utility with its
        <emphasis>-r</emphasis> option specified, but with some additional benefits, such as
        using pathspec patterns to limit paths;  see the <emphasis>pathspec</emphasis> entry
        in <xref linkend="gitglossary(7)" /> for more information.
</simpara>
<simpara>This option cannot be used together with <emphasis>--cached</emphasis> or <emphasis>--untracked</emphasis>.
See also <emphasis>grep.fallbackToNoIndex</emphasis> in <emphasis>CONFIGURATION</emphasis> below.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--no-exclude-standard
</term>
<listitem>
<simpara>
        Also search in ignored files by not honoring the <emphasis>.gitignore</emphasis>
        mechanism. Only useful with <emphasis>--untracked</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--exclude-standard
</term>
<listitem>
<simpara>
        Do not pay attention to ignored files specified via the <emphasis>.gitignore</emphasis>
        mechanism.  Only useful when searching files in the current directory
        with <emphasis>--no-index</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--recurse-submodules
</term>
<listitem>
<simpara>
        Recursively search in each submodule that is active and
        checked out in the repository.  When used in combination with the
        <emphasis>&lt;tree&gt;</emphasis> option the prefix of all submodule output will be the name of
        the parent project's <emphasis>&lt;tree&gt;</emphasis> object.  This option cannot be used together
        with <emphasis>--untracked</emphasis>, and it has no effect if <emphasis>--no-index</emphasis> is specified.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-a
</term>
<term>
--text
</term>
<listitem>
<simpara>
        Process binary files as if they were text.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--textconv
</term>
<listitem>
<simpara>
        Honor textconv filter settings.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--no-textconv
</term>
<listitem>
<simpara>
        Do not honor textconv filter settings.
        This is the default.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-i
</term>
<term>
--ignore-case
</term>
<listitem>
<simpara>
        Ignore case differences between the patterns and the
        files.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-I
</term>
<listitem>
<simpara>
        Don't match the pattern in binary files.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--max-depth &lt;depth&gt;
</term>
<listitem>
<simpara>
        For each &lt;pathspec&gt; given on command line, descend at most &lt;depth&gt;
        levels of directories. A value of -1 means no limit.
        This option is ignored if &lt;pathspec&gt; contains active wildcards.
        In other words if "a*" matches a directory named "a*",
        "*" is matched literally so --max-depth is still effective.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-r
</term>
<term>
--recursive
</term>
<listitem>
<simpara>
        Same as <emphasis>--max-depth=-1</emphasis>; this is the default.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--no-recursive
</term>
<listitem>
<simpara>
        Same as <emphasis>--max-depth=0</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-w
</term>
<term>
--word-regexp
</term>
<listitem>
<simpara>
        Match the pattern only at word boundary (either begin at the
        beginning of a line, or preceded by a non-word character; end at
        the end of a line or followed by a non-word character).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-v
</term>
<term>
--invert-match
</term>
<listitem>
<simpara>
        Select non-matching lines.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-h
</term>
<term>
-H
</term>
<listitem>
<simpara>
        By default, the command shows the filename for each
        match.  <emphasis>-h</emphasis> option is used to suppress this output.
        <emphasis>-H</emphasis> is there for completeness and does not do anything
        except it overrides <emphasis>-h</emphasis> given earlier on the command
        line.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--full-name
</term>
<listitem>
<simpara>
        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.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-E
</term>
<term>
--extended-regexp
</term>
<term>
-G
</term>
<term>
--basic-regexp
</term>
<listitem>
<simpara>
        Use POSIX extended/basic regexp for patterns.  Default
        is to use basic regexp.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-P
</term>
<term>
--perl-regexp
</term>
<listitem>
<simpara>
        Use Perl-compatible regular expressions for patterns.
</simpara>
<simpara>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.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-F
</term>
<term>
--fixed-strings
</term>
<listitem>
<simpara>
        Use fixed strings for patterns (don't interpret pattern
        as a regex).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-n
</term>
<term>
--line-number
</term>
<listitem>
<simpara>
        Prefix the line number to matching lines.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--column
</term>
<listitem>
<simpara>
        Prefix the 1-indexed byte-offset of the first match from the start of the
        matching line.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-l
</term>
<term>
--files-with-matches
</term>
<term>
--name-only
</term>
<term>
-L
</term>
<term>
--files-without-match
</term>
<listitem>
<simpara>
        Instead of showing every matched line, show only the
        names of files that contain (or do not contain) matches.
        For better compatibility with <emphasis>git diff</emphasis>, <emphasis>--name-only</emphasis> is a
        synonym for <emphasis>--files-with-matches</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-O[&lt;pager&gt;]
</term>
<term>
--open-files-in-pager[=&lt;pager&gt;]
</term>
<listitem>
<simpara>
        Open the matching files in the pager (not the output of <emphasis>grep</emphasis>).
        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 <emphasis>pager</emphasis> argument is
        optional; if specified, it must be stuck to the option
        without a space. If <emphasis>pager</emphasis> is unspecified, the default pager
        will be used (see <emphasis>core.pager</emphasis> in <xref linkend="git-config(1)" />).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-z
</term>
<term>
--null
</term>
<listitem>
<simpara>
        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 <emphasis>core.quotePath</emphasis> (see <xref linkend="git-config(1)" />).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-o
</term>
<term>
--only-matching
</term>
<listitem>
<simpara>
        Print only the matched (non-empty) parts of a matching line, with each such
        part on a separate output line.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-c
</term>
<term>
--count
</term>
<listitem>
<simpara>
        Instead of showing every matched line, show the number of
        lines that match.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--color[=&lt;when&gt;]
</term>
<listitem>
<simpara>
        Show colored matches.
        The value must be always (the default), never, or auto.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--no-color
</term>
<listitem>
<simpara>
        Turn off match highlighting, even when the configuration file
        gives the default to color output.
        Same as <emphasis>--color=never</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--break
</term>
<listitem>
<simpara>
        Print an empty line between matches from different files.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--heading
</term>
<listitem>
<simpara>
        Show the filename above the matches in that file instead of
        at the start of each shown line.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-p
</term>
<term>
--show-function
</term>
<listitem>
<simpara>
        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 <emphasis>git diff</emphasis> works out
        patch hunk headers (see <emphasis>Defining a custom hunk-header</emphasis> in
        <xref linkend="gitattributes(5)" />).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-&lt;num&gt;
</term>
<term>
-C &lt;num&gt;
</term>
<term>
--context &lt;num&gt;
</term>
<listitem>
<simpara>
        Show &lt;num&gt; leading and trailing lines, and place a line
        containing <emphasis>--</emphasis> between contiguous groups of matches.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-A &lt;num&gt;
</term>
<term>
--after-context &lt;num&gt;
</term>
<listitem>
<simpara>
        Show &lt;num&gt; trailing lines, and place a line containing
        <emphasis>--</emphasis> between contiguous groups of matches.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-B &lt;num&gt;
</term>
<term>
--before-context &lt;num&gt;
</term>
<listitem>
<simpara>
        Show &lt;num&gt; leading lines, and place a line containing
        <emphasis>--</emphasis> between contiguous groups of matches.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-W
</term>
<term>
--function-context
</term>
<listitem>
<simpara>
        Show the surrounding text from the previous line containing a
        function name up to the one before the next function name,
        effectively showing the whole function in which the match was
        found. The function names are determined in the same way as
        <emphasis>git diff</emphasis> works out patch hunk headers (see <emphasis>Defining a
        custom hunk-header</emphasis> in <xref linkend="gitattributes(5)" />).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-m &lt;num&gt;
</term>
<term>
--max-count &lt;num&gt;
</term>
<listitem>
<simpara>
        Limit the amount of matches per file. When using the <emphasis>-v</emphasis> or
        <emphasis>--invert-match</emphasis> option, the search stops after the specified
        number of non-matches. A value of -1 will return unlimited
        results (the default). A value of 0 will exit immediately with
        a non-zero status.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--threads &lt;num&gt;
</term>
<listitem>
<simpara>
        Number of <emphasis>grep</emphasis> worker threads to use.  See <emphasis>NOTES ON THREADS</emphasis>
        and <emphasis>grep.threads</emphasis> in <emphasis>CONFIGURATION</emphasis> for more information.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-f &lt;file&gt;
</term>
<listitem>
<simpara>
        Read patterns from &lt;file&gt;, one per line.
</simpara>
<simpara>Passing the pattern via &lt;file&gt; allows for providing a search pattern
containing a \0.</simpara>
<simpara>Not all pattern types support patterns containing \0. Git will error
out if a given pattern type can't support such a pattern. The
<emphasis>--perl-regexp</emphasis> pattern type when compiled against the PCRE v2 backend
has the widest support for these types of patterns.</simpara>
<simpara>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 <emphasis>--ignore-case</emphasis>.</simpara>
<simpara>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.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-e
</term>
<listitem>
<simpara>
        The next parameter is the pattern. This option has to be
        used for patterns starting with <emphasis>-</emphasis> and should be used in
        scripts passing user input to grep.  Multiple patterns are
        combined by <emphasis>or</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--and
</term>
<term>
--or
</term>
<term>
--not
</term>
<term>
( &#8230; )
</term>
<listitem>
<simpara>
        Specify how multiple patterns are combined using Boolean
        expressions.  <emphasis>--or</emphasis> is the default operator.  <emphasis>--and</emphasis> has
        higher precedence than <emphasis>--or</emphasis>.  <emphasis>-e</emphasis> has to be used for all
        patterns.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--all-match
</term>
<listitem>
<simpara>
        When giving multiple pattern expressions combined with <emphasis>--or</emphasis>,
        this flag is specified to limit the match to files that
        have lines to match all of them.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-q
</term>
<term>
--quiet
</term>
<listitem>
<simpara>
        Do not output matched lines; instead, exit with status 0 when
        there is a match and with non-zero status when there isn't.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;tree&gt;&#8230;
</term>
<listitem>
<simpara>
        Instead of searching tracked files in the working tree, search
        blobs in the given trees.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--
</term>
<listitem>
<simpara>
        Signals the end of options; the rest of the parameters
        are &lt;pathspec&gt; limiters.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;pathspec&gt;&#8230;
</term>
<listitem>
<simpara>
        If given, limit the search to paths matching at least one pattern.
        Both leading paths match and glob(7) patterns are supported.
</simpara>
<simpara>For more details about the &lt;pathspec&gt; syntax, see the <emphasis>pathspec</emphasis> entry
in <xref linkend="gitglossary(7)" />.</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-grep(1)__examples">
<title>EXAMPLES</title>
<variablelist>
<varlistentry>
<term>
<emphasis>git grep 'time_t' -- '*.[ch]'</emphasis>
</term>
<listitem>
<simpara>
        Looks for <emphasis>time_t</emphasis> in all tracked .c and .h files in the working
        directory and its subdirectories.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<emphasis>git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)</emphasis>
</term>
<listitem>
<simpara>
        Looks for a line that has <emphasis>#define</emphasis> and either <emphasis>MAX_PATH</emphasis> or
        <emphasis>PATH_MAX</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<emphasis>git grep --all-match -e NODE -e Unexpected</emphasis>
</term>
<listitem>
<simpara>
        Looks for a line that has <emphasis>NODE</emphasis> or <emphasis>Unexpected</emphasis> in
        files that have lines that match both.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<emphasis>git grep solution -- :^Documentation</emphasis>
</term>
<listitem>
<simpara>
        Looks for <emphasis>solution</emphasis>, excluding files in <emphasis>Documentation</emphasis>.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-grep(1)__notes_on_threads">
<title>NOTES ON THREADS</title>
<simpara>The <emphasis>--threads</emphasis> option (and the <emphasis>grep.threads</emphasis> configuration) will be ignored when
<emphasis>--open-files-in-pager</emphasis> is used, forcing a single-threaded execution.</simpara>
<simpara>When grepping the object store (with <emphasis>--cached</emphasis> or giving tree objects), running
with multiple threads might perform slower than single-threaded if <emphasis>--textconv</emphasis>
is given and there are too many text conversions.  Thus, if low performance is
experienced in this case, it might be desirable to use <emphasis>--threads=1</emphasis>.</simpara>
</simplesect>
<simplesect id="git-grep(1)__configuration">
<title>CONFIGURATION</title>
<simpara>Everything below this line in this section is selectively included
from the <xref linkend="git-config(1)" /> documentation. The content is the same
as what's found there:</simpara>
<variablelist>
<varlistentry>
<term>
grep.lineNumber
</term>
<listitem>
<simpara>
        If set to true, enable <emphasis>-n</emphasis> option by default.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
grep.column
</term>
<listitem>
<simpara>
        If set to true, enable the <emphasis>--column</emphasis> option by default.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
grep.patternType
</term>
<listitem>
<simpara>
        Set the default matching behavior. Using a value of <emphasis>basic</emphasis>, <emphasis>extended</emphasis>,
        <emphasis>fixed</emphasis>, or <emphasis>perl</emphasis> will enable the <emphasis>--basic-regexp</emphasis>, <emphasis>--extended-regexp</emphasis>,
        <emphasis>--fixed-strings</emphasis>, or <emphasis>--perl-regexp</emphasis> option accordingly, while the
        value <emphasis>default</emphasis> will use the <emphasis>grep.extendedRegexp</emphasis> option to choose
        between <emphasis>basic</emphasis> and <emphasis>extended</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
grep.extendedRegexp
</term>
<listitem>
<simpara>
        If set to true, enable <emphasis>--extended-regexp</emphasis> option by default. This
        option is ignored when the <emphasis>grep.patternType</emphasis> option is set to a value
        other than <emphasis>default</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
grep.threads
</term>
<listitem>
<simpara>
        Number of grep worker threads to use. If unset (or set to 0), Git will
        use as many threads as the number of logical cores available.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
grep.fullName
</term>
<listitem>
<simpara>
        If set to true, enable <emphasis>--full-name</emphasis> option by default.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
grep.fallbackToNoIndex
</term>
<listitem>
<simpara>
        If set to true, fall back to <emphasis>git grep --no-index</emphasis> if <emphasis>git grep</emphasis>
        is executed outside of a git repository.  Defaults to false.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-grep(1)__git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)" /> suite</simpara>
</simplesect>
</sect2>
