Git Reference ยท Remotes and Collaboration
git apply command
Reference for git apply: apply unified diffs to the working tree or index, check them first with --check and --stat, reverse them, and handle rejects.
git apply takes a unified diff (from git diff, diff -u or a patch file) and applies it to files. By default it changes the working tree only. It does not create commits and does not read mail headers, which makes it the right tool for plain diffs; for patches with an author and message use git am.
Its main safety feature is that it is all-or-nothing: if any hunk of any file in the patch fails, nothing is changed, unless you ask for partial application with --reject.
git apply [options] [<patch>...]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
| Option | Description |
|---|---|
<patch>... |
Patch files to read. With none (or -) the patch is read from standard input. |
--stat |
Show a diffstat instead of applying. |
--numstat, --summary |
Machine-readable counts, or a summary of creates, deletes, renames and mode changes. |
--check |
Report whether the patch would apply; change nothing. |
--index |
Apply to both the working tree and the index. The index must match the working tree for the affected files. |
--cached |
Apply to the index only; the working tree is untouched. |
-3, --3way |
Try a three-way merge using blob IDs; conflicts are left in the files and the index (implies --index). |
-R, --reverse |
Apply the patch backwards. |
--reject |
Apply the hunks that fit and save the failed ones to <file>.rej. Exits non-zero if anything was rejected. |
-p<n> |
Strip n leading path components (default 1, to remove a/ and b/). |
-C<n> |
Require at least n lines of matching context (lower is looser). |
--directory=<root> |
Prepend a directory to every path in the patch. |
--include=<pattern>, --exclude=<pattern> |
Limit which files of the patch are applied. |
--whitespace=(nowarn|warn|fix|error|error-all) |
How to treat added lines with whitespace errors. |
--ignore-whitespace |
Ignore whitespace differences when matching context. |
--unidiff-zero |
Accept patches made with zero lines of context (diff -U0). |
--allow-empty |
Do not fail on a patch with no changes. |
-v, -q |
More or less output. |
Exit status: 0 on success; 1 if the patch cannot be applied (or --reject rejected something).
Usage notes
- Outside a repository
git applyworks as a stricterpatch: it applies diffs to files in the current directory. - No new commit. To record the result, stage and commit it. To keep the original authorship, use git am.
--indexversus--cached.--indexfails if the working-tree file differs from the index.--cachedignores the working tree, which suits automation that builds a commit without touching files.- Renames and modes. Git-style diffs carry renames, copies, mode changes and binary data (with
--binaryat diff time); plaindiff -uoutput cannot. - Whitespace. The
core.whitespacesetting and--whitespace=fixcan clean trailing whitespace while applying. See core.whitespace. - Prefer
--checkin scripts before applying, and--3wayfor patches generated against a slightly different base. - Reading diffs from a pipe (
git diff other | git apply) is a quick way to move changes between clones, andgit diff | git apply -Rundoes them.