Skip to content

apply Git Command Guide

The git apply command applies patches (in unified diff format) to files in the working directory. Unlike git am, it does not create commits but applies the changes directly to tracked files. It’s useful for reviewing and applying patches before committing, or for merging external contributions.

Terminal window
git apply [options] [<patch>...]
OptionDescription
-pStrip n leading components from patch paths
-RApply patches in reverse (undo changes)
—checkOnly check if patches can be applied, don’t apply
—statInstead of applying, show diffstat of patches
-NHonor renames in the patch
—whitespace=Handle whitespace errors
-CEnsure at least n lines of surrounding context
—ignore-space-changeIgnore changes in whitespace amount
—ignore-whitespaceIgnore all whitespace changes
-vBe verbose
ParameterDescription
patchPatch file(s) to apply
Terminal window
git apply fix-bug.patch

Applies the changes in fix-bug.patch to the working directory.

Terminal window
git apply --check feature.patch

Verifies that the patch can be applied without errors, without actually applying it.

Terminal window
git apply --stat changes.patch

Displays a diffstat showing what the patch would change.

Terminal window
git apply --ignore-whitespace patch.diff

Applies the patch while ignoring any whitespace changes.

Terminal window
git apply -R revert.patch

Undoes the changes introduced by the patch.

How do I apply a patch file using git apply?

Section titled “How do I apply a patch file using git apply?”

To apply a patch file using git apply, use the following command:

Terminal window
git apply <patch-file>

How can I check if a patch can be applied without making changes?

Section titled “How can I check if a patch can be applied without making changes?”

To check if a patch applies cleanly without actually applying it, execute:

Terminal window
git apply --check <patch-file>

How do I view the changes a patch would make without applying it?

Section titled “How do I view the changes a patch would make without applying it?”

To see the statistics of what a patch changes without applying it, use:

Terminal window
git apply --stat <patch-file>

How can I apply a patch while ignoring whitespace differences?

Section titled “How can I apply a patch while ignoring whitespace differences?”

To apply a patch ignoring whitespace changes, use:

Terminal window
git apply --ignore-whitespace <patch-file>

How do I reverse apply a patch using git apply?

Section titled “How do I reverse apply a patch using git apply?”

To undo a patch, apply it in reverse with:

Terminal window
git apply -R <patch-file>

How can I handle renames in patches with git apply?

Section titled “How can I handle renames in patches with git apply?”

To honor renames mentioned in the patch, use:

Terminal window
git apply -N <patch-file>
  1. Applying a simple diff patch to files
  2. Checking if a patch can be applied without making changes
  3. Previewing the impact of a patch before applying
  4. Applying a patch while ignoring whitespace changes
  5. Reversing the application of a previously applied patch
  6. Stripping leading path components from patch files
  7. Being verbose during the patch application process