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

<sect2 lang="en" id="git-apply(1)">
    <title>git-apply(1)</title>
    <date>2024-07-02</date>
<revhistory><revision><date>2024-07-02</date></revision></revhistory>
<indexterm>
<primary>git-apply(1)</primary>
</indexterm>
<simplesect id="git-apply(1)__name">
<title>NAME</title>
<simpara>git-apply - Apply a patch to files and/or to the index</simpara>
</simplesect>
<simplesect id="git-apply(1)__synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git apply</emphasis> [--stat] [--numstat] [--summary] [--check] [--index | --intent-to-add] [--3way]
          [--apply] [--no-add] [--build-fake-ancestor=&lt;file&gt;] [-R | --reverse]
          [--allow-binary-replacement | --binary] [--reject] [-z]
          [-p&lt;n&gt;] [-C&lt;n&gt;] [--inaccurate-eof] [--recount] [--cached]
          [--ignore-space-change | --ignore-whitespace]
          [--whitespace=(nowarn|warn|fix|error|error-all)]
          [--exclude=&lt;path&gt;] [--include=&lt;path&gt;] [--directory=&lt;root&gt;]
          [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [&lt;patch&gt;&#8230;]</literallayout>
</blockquote>
</simplesect>
<simplesect id="git-apply(1)__description">
<title>DESCRIPTION</title>
<simpara>Reads the supplied diff output (i.e. "a patch") and applies it to files.
When running from a subdirectory in a repository, patched paths
outside the directory are ignored.
With the <emphasis>--index</emphasis> option, the patch is also applied to the index, and
with the <emphasis>--cached</emphasis> option, the patch is only applied to the index.
Without these options, the command applies the patch only to files,
and does not require them to be in a Git repository.</simpara>
<simpara>This command applies the patch but does not create a commit.  Use
<xref linkend="git-am(1)" /> to create commits from patches generated by
<xref linkend="git-format-patch(1)" /> and/or received by email.</simpara>
</simplesect>
<simplesect id="git-apply(1)__options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
&lt;patch&gt;&#8230;
</term>
<listitem>
<simpara>
        The files to read the patch from.  <emphasis>-</emphasis> can be used to read
        from the standard input.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--stat
</term>
<listitem>
<simpara>
        Instead of applying the patch, output diffstat for the
        input.  Turns off "apply".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--numstat
</term>
<listitem>
<simpara>
        Similar to <emphasis>--stat</emphasis>, but shows the number of added and
        deleted lines in decimal notation and the pathname without
        abbreviation, to make it more machine friendly.  For
        binary files, outputs two <emphasis>-</emphasis> instead of saying
        <emphasis>0 0</emphasis>.  Turns off "apply".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--summary
</term>
<listitem>
<simpara>
        Instead of applying the patch, output a condensed
        summary of information obtained from git diff extended
        headers, such as creations, renames, and mode changes.
        Turns off "apply".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--check
</term>
<listitem>
<simpara>
        Instead of applying the patch, see if the patch is
        applicable to the current working tree and/or the index
        file and detects errors.  Turns off "apply".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--index
</term>
<listitem>
<simpara>
        Apply the patch to both the index and the working tree (or
        merely check that it would apply cleanly to both if <emphasis>--check</emphasis> is
        in effect). Note that <emphasis>--index</emphasis> expects index entries and
        working tree copies for relevant paths to be identical (their
        contents and metadata such as file mode must match), and will
        raise an error if they are not, even if the patch would apply
        cleanly to both the index and the working tree in isolation.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--cached
</term>
<listitem>
<simpara>
        Apply the patch to just the index, without touching the working
        tree. If <emphasis>--check</emphasis> is in effect, merely check that it would
        apply cleanly to the index entry.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--intent-to-add
</term>
<listitem>
<simpara>
        When applying the patch only to the working tree, mark new
        files to be added to the index later (see <emphasis>--intent-to-add</emphasis>
        option in <xref linkend="git-add(1)" />). This option is ignored unless
        running in a Git repository and <emphasis>--index</emphasis> is not specified.
        Note that <emphasis>--index</emphasis> could be implied by other options such
        as <emphasis>--cached</emphasis> or <emphasis>--3way</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-3
</term>
<term>
--3way
</term>
<listitem>
<simpara>
        Attempt 3-way merge if the patch records the identity of blobs it is supposed
        to apply to and we have those blobs available locally, possibly leaving the
        conflict markers in the files in the working tree for the user to
        resolve.  This option implies the <emphasis>--index</emphasis> option unless the
        <emphasis>--cached</emphasis> option is used, and is incompatible with the <emphasis>--reject</emphasis> option.
        When used with the <emphasis>--cached</emphasis> option, any conflicts are left at higher stages
        in the cache.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--build-fake-ancestor=&lt;file&gt;
</term>
<listitem>
<simpara>
        Newer <emphasis>git diff</emphasis> output has embedded <emphasis>index information</emphasis>
        for each blob to help identify the original version that
        the patch applies to.  When this flag is given, and if
        the original versions of the blobs are available locally,
        builds a temporary index containing those blobs.
</simpara>
<simpara>When a pure mode change is encountered (which has no index information),
the information is read from the current index instead.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-R
</term>
<term>
--reverse
</term>
<listitem>
<simpara>
        Apply the patch in reverse.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--reject
</term>
<listitem>
<simpara>
        For atomicity, <emphasis>git apply</emphasis> by default fails the whole patch and
        does not touch the working tree when some of the hunks
        do not apply.  This option makes it apply
        the parts of the patch that are applicable, and leave the
        rejected hunks in corresponding *.rej files.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-z
</term>
<listitem>
<simpara>
        When <emphasis>--numstat</emphasis> has been given, do not munge pathnames,
        but use a NUL-terminated machine-readable format.
</simpara>
<simpara>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>
-p&lt;n&gt;
</term>
<listitem>
<simpara>
        Remove &lt;n&gt; leading path components (separated by slashes) from
        traditional diff paths. E.g., with <emphasis>-p2</emphasis>, a patch against
        <emphasis>a/dir/file</emphasis> will be applied directly to <emphasis>file</emphasis>. The default is
        1.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-C&lt;n&gt;
</term>
<listitem>
<simpara>
        Ensure at least &lt;n&gt; lines of surrounding context match before
        and after each change.  When fewer lines of surrounding
        context exist they all must match.  By default no context is
        ever ignored.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--unidiff-zero
</term>
<listitem>
<simpara>
        By default, <emphasis>git apply</emphasis> expects that the patch being
        applied is a unified diff with at least one line of context.
        This provides good safety measures, but breaks down when
        applying a diff generated with <emphasis>--unified=0</emphasis>. To bypass these
        checks use <emphasis>--unidiff-zero</emphasis>.
</simpara>
<simpara>Note, for the reasons stated above, the usage of context-free patches is
discouraged.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--apply
</term>
<listitem>
<simpara>
        If you use any of the options marked "Turns off
        <emphasis>apply</emphasis>" above, <emphasis>git apply</emphasis> reads and outputs the
        requested information without actually applying the
        patch.  Give this flag after those flags to also apply
        the patch.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--no-add
</term>
<listitem>
<simpara>
        When applying a patch, ignore additions made by the
        patch.  This can be used to extract the common part between
        two files by first running <emphasis>diff</emphasis> on them and applying
        the result with this option, which would apply the
        deletion part but not the addition part.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--allow-binary-replacement
</term>
<term>
--binary
</term>
<listitem>
<simpara>
        Historically we did not allow binary patch application
        without an explicit permission from the user, and this
        flag was the way to do so.  Currently, we always allow binary
        patch application, so this is a no-op.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--exclude=&lt;path-pattern&gt;
</term>
<listitem>
<simpara>
        Don't apply changes to files matching the given path pattern. This can
        be useful when importing patchsets, where you want to exclude certain
        files or directories.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--include=&lt;path-pattern&gt;
</term>
<listitem>
<simpara>
        Apply changes to files matching the given path pattern. This can
        be useful when importing patchsets, where you want to include certain
        files or directories.
</simpara>
<simpara>When <emphasis>--exclude</emphasis> and <emphasis>--include</emphasis> patterns are used, they are examined in the
order they appear on the command line, and the first match determines if a
patch to each path is used.  A patch to a path that does not match any
include/exclude pattern is used by default if there is no include pattern
on the command line, and ignored if there is any include pattern.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--ignore-space-change
</term>
<term>
--ignore-whitespace
</term>
<listitem>
<simpara>
        When applying a patch, ignore changes in whitespace in context
        lines if necessary.
        Context lines will preserve their whitespace, and they will not
        undergo whitespace fixing regardless of the value of the
        <emphasis>--whitespace</emphasis> option. New lines will still be fixed, though.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--whitespace=&lt;action&gt;
</term>
<listitem>
<simpara>
        When applying a patch, detect a new or modified line that has
        whitespace errors.  What are considered whitespace errors is
        controlled by <emphasis>core.whitespace</emphasis> configuration.  By default,
        trailing whitespaces (including lines that solely consist of
        whitespaces) and a space character that is immediately followed
        by a tab character inside the initial indent of the line are
        considered whitespace errors.
</simpara>
<simpara>By default, the command outputs warning messages but applies the patch.
When <emphasis>git-apply</emphasis> is used for statistics and not applying a
patch, it defaults to <emphasis>nowarn</emphasis>.</simpara>
<simpara>You can use different <emphasis>&lt;action&gt;</emphasis> values to control this
behavior:</simpara>
<itemizedlist>
<listitem>
<simpara>
<emphasis>nowarn</emphasis> turns off the trailing whitespace warning.
</simpara>
</listitem>
<listitem>
<simpara>
<emphasis>warn</emphasis> outputs warnings for a few such errors, but applies the
  patch as-is (default).
</simpara>
</listitem>
<listitem>
<simpara>
<emphasis>fix</emphasis> outputs warnings for a few such errors, and applies the
  patch after fixing them (<emphasis>strip</emphasis> is a synonym -- the tool
  used to consider only trailing whitespace characters as errors, and the
  fix involved <emphasis>stripping</emphasis> them, but modern Gits do more).
</simpara>
</listitem>
<listitem>
<simpara>
<emphasis>error</emphasis> outputs warnings for a few such errors, and refuses
  to apply the patch.
</simpara>
</listitem>
<listitem>
<simpara>
<emphasis>error-all</emphasis> is similar to <emphasis>error</emphasis> but shows all errors.
</simpara>
</listitem>
</itemizedlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
--inaccurate-eof
</term>
<listitem>
<simpara>
        Under certain circumstances, some versions of <emphasis>diff</emphasis> do not correctly
        detect a missing new-line at the end of the file. As a result, patches
        created by such <emphasis>diff</emphasis> programs do not record incomplete lines
        correctly. This option adds support for applying such patches by
        working around this bug.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-v
</term>
<term>
--verbose
</term>
<listitem>
<simpara>
        Report progress to stderr. By default, only a message about the
        current patch being applied will be printed. This option will cause
        additional information to be reported.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-q
</term>
<term>
--quiet
</term>
<listitem>
<simpara>
        Suppress stderr output. Messages about patch status and progress
        will not be printed.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--recount
</term>
<listitem>
<simpara>
        Do not trust the line counts in the hunk headers, but infer them
        by inspecting the patch (e.g. after editing the patch without
        adjusting the hunk headers appropriately).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--directory=&lt;root&gt;
</term>
<listitem>
<simpara>
        Prepend &lt;root&gt; to all filenames.  If a "-p" argument was also passed,
        it is applied before prepending the new root.
</simpara>
<simpara>For example, a patch that talks about updating <emphasis>a/git-gui.sh</emphasis> to <emphasis>b/git-gui.sh</emphasis>
can be applied to the file in the working tree <emphasis>modules/git-gui/git-gui.sh</emphasis> by
running <emphasis>git apply --directory=modules/git-gui</emphasis>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--unsafe-paths
</term>
<listitem>
<simpara>
        By default, a patch that affects outside the working area
        (either a Git controlled working tree, or the current working
        directory when "git apply" is used as a replacement of GNU
        patch) is rejected as a mistake (or a mischief).
</simpara>
<simpara>When <emphasis>git apply</emphasis> is used as a "better GNU patch", the user can pass
the <emphasis>--unsafe-paths</emphasis> option to override this safety check.  This option
has no effect when <emphasis>--index</emphasis> or <emphasis>--cached</emphasis> is in use.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--allow-empty
</term>
<listitem>
<simpara>
        Don't return an error for patches containing no diff. This includes
        empty patches and patches with commit text only.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-apply(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>
apply.ignoreWhitespace
</term>
<listitem>
<simpara>
        When set to <emphasis>change</emphasis>, tells <emphasis>git apply</emphasis> to ignore changes in
        whitespace, in the same way as the <emphasis>--ignore-space-change</emphasis>
        option.
        When set to one of: no, none, never, false, it tells <emphasis>git apply</emphasis> to
        respect all whitespace differences.
        See <xref linkend="git-apply(1)" />.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
apply.whitespace
</term>
<listitem>
<simpara>
        Tells <emphasis>git apply</emphasis> how to handle whitespace, in the same way
        as the <emphasis>--whitespace</emphasis> option. See <xref linkend="git-apply(1)" />.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-apply(1)__submodules">
<title>SUBMODULES</title>
<simpara>If the patch contains any changes to submodules then <emphasis>git apply</emphasis>
treats these changes as follows.</simpara>
<simpara>If <emphasis>--index</emphasis> is specified (explicitly or implicitly), then the submodule
commits must match the index exactly for the patch to apply.  If any
of the submodules are checked-out, then these check-outs are completely
ignored, i.e., they are not required to be up to date or clean and they
are not updated.</simpara>
<simpara>If <emphasis>--index</emphasis> is not specified, then the submodule commits in the patch
are ignored and only the absence or presence of the corresponding
subdirectory is checked and (if possible) updated.</simpara>
</simplesect>
<simplesect id="git-apply(1)__see_also">
<title>SEE ALSO</title>
<simpara><xref linkend="git-am(1)" />.</simpara>
</simplesect>
<simplesect id="git-apply(1)__git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)" /> suite</simpara>
</simplesect>
</sect2>
