<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="git-apply(1)">
<articleinfo>
    <title>git-apply(1)</title>
        <indexterm>
                <primary>git-apply(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-apply - Apply a patch on a git index file and a working tree</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git apply</emphasis> [--stat] [--numstat] [--summary] [--check] [--index]
          [--apply] [--no-add] [--build-fake-ancestor=&lt;file&gt;] [-R | --reverse]
          [--allow-binary-replacement | --binary] [--reject] [-z]
          [-pNUM] [-CNUM] [--inaccurate-eof] [--recount] [--cached]
          [--whitespace=&lt;nowarn|warn|fix|error|error-all&gt;]
          [--exclude=PATH] [--include=PATH] [--directory=&lt;root&gt;]
          [--verbose] [&lt;patch&gt;&#8230;]</literallayout>
</blockquote>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>Reads supplied <emphasis>diff</emphasis> output and applies it on a git index file
and a work tree.</simpara>
</simplesect>
<simplesect id="_options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
&lt;patch&gt;&#8230;
</term>
<listitem>
<simpara>
        The files to read 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 --stat, but shows number of added and
        deleted lines in decimal notation and pathname without
        abbreviation, to make it more machine friendly.  For
        binary files, outputs two <literal>-</literal> instead of saying
        <literal>0 0</literal>.  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 work tree and/or the index
        file and detects errors.  Turns off "apply".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--index
</term>
<listitem>
<simpara>
        When --check is in effect, or when applying the patch
        (which is the default when none of the options that
        disables it is in effect), make sure the patch is
        applicable to what the current index file records.  If
        the file to be patched in the work tree is not
        up-to-date, it is flagged as an error.  This flag also
        causes the index file to be updated.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--cached
</term>
<listitem>
<simpara>
        Apply a patch without touching the working tree. Instead, take the
        cached data, apply the patch, and store the result in the index,
        without using the working tree. This implies <emphasis>--index</emphasis>.
</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 is 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 showing the index information, do not munge paths,
        but use NUL terminated machine readable format.  Without
        this flag, the pathnames output will have TAB, LF, and
        backslash characters replaced with <literal>\t</literal>, <literal>\n</literal>, and <literal>\\</literal>,
        respectively.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-p&lt;n&gt;
</term>
<listitem>
<simpara>
        Remove &lt;n&gt; leading slashes from traditional diff paths. 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 --unified=0. To bypass these
        checks use <emphasis>--unidiff-zero</emphasis>.
</simpara>
<simpara>Note, for the reasons stated above usage of context-free patches are
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
        information you asked 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 addition part.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--allow-binary-replacement
</term>
<term>
--binary
</term>
<listitem>
<simpara>
        Historically we did not allow binary patch applied
        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&#8217;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 --exclude and --include 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>
--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 <literal>core.whitespace</literal> 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 <literal>git-apply is used for statistics and not applying a
patch, it defaults to `nowarn</literal>.</simpara>
<simpara>You can use different <literal>&lt;action&gt;</literal> to control this
behavior:</simpara>
<itemizedlist>
<listitem>
<simpara>
<literal>nowarn</literal> turns off the trailing whitespace warning.
</simpara>
</listitem>
<listitem>
<simpara>
<literal>warn</literal> outputs warnings for a few such errors, but applies the
  patch as-is (default).
</simpara>
</listitem>
<listitem>
<simpara>
<literal>fix</literal> outputs warnings for a few such errors, and applies the
  patch after fixing them (<literal>strip</literal> is a synonym --- the tool
  used to consider only trailing whitespaces as errors, and the
  fix involved <emphasis>stripping</emphasis> them, but modern gits do more).
</simpara>
</listitem>
<listitem>
<simpara>
<literal>error</literal> outputs warnings for a few such errors, and refuses
  to apply the patch.
</simpara>
</listitem>
<listitem>
<simpara>
<literal>error-all</literal> is similar to <literal>error</literal> 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>
--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 passed, too,
        it is applied before prepending the new root.
</simpara>
<simpara>For example, a patch that talks about updating <literal>a/git-gui.sh</literal> to <literal>b/git-gui.sh</literal>
can be applied to the file in the working tree <literal>modules/git-gui/git-gui.sh</literal> by
running <literal>git apply --directory=modules/git-gui</literal>.</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_configuration">
<title>Configuration</title>
<variablelist>
<varlistentry>
<term>
apply.whitespace
</term>
<listitem>
<simpara>
        When no <literal>--whitespace</literal> flag is given from the command
        line, this configuration item is used as the default.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_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 --index 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 --index is not specified, then the submodule commits in the patch
are ignored and only the absence of presence of the corresponding
subdirectory is checked and (if possible) updated.</simpara>
</simplesect>
<simplesect id="_author">
<title>Author</title>
<simpara>Written by Linus Torvalds &lt;<ulink url="mailto:torvalds@osdl.org">torvalds@osdl.org</ulink>&gt;</simpara>
</simplesect>
<simplesect id="_documentation">
<title>Documentation</title>
<simpara>Documentation by Junio C Hamano</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
