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.
git apply Syntax:
Section titled “git apply Syntax:”git apply [options] [<patch>...]Options:
Section titled “Options:”| Option | Description |
|---|---|
| -p | Strip n leading components from patch paths |
| -R | Apply patches in reverse (undo changes) |
| —check | Only check if patches can be applied, don’t apply |
| —stat | Instead of applying, show diffstat of patches |
| -N | Honor renames in the patch |
| —whitespace= | Handle whitespace errors |
| -C | Ensure at least n lines of surrounding context |
| —ignore-space-change | Ignore changes in whitespace amount |
| —ignore-whitespace | Ignore all whitespace changes |
| -v | Be verbose |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| patch | Patch file(s) to apply |
git apply Command Samples:
Section titled “git apply Command Samples:”Apply a patch file
Section titled “Apply a patch file”git apply fix-bug.patchApplies the changes in fix-bug.patch to the working directory.
Check if a patch can be applied
Section titled “Check if a patch can be applied”git apply --check feature.patchVerifies that the patch can be applied without errors, without actually applying it.
Show statistics of a patch
Section titled “Show statistics of a patch”git apply --stat changes.patchDisplays a diffstat showing what the patch would change.
Apply patch ignoring whitespace
Section titled “Apply patch ignoring whitespace”git apply --ignore-whitespace patch.diffApplies the patch while ignoring any whitespace changes.
Apply patches in reverse
Section titled “Apply patches in reverse”git apply -R revert.patchUndoes 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:
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:
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:
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:
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:
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:
git apply -N <patch-file>Applications of the git apply command
Section titled “Applications of the git apply command”- Applying a simple diff patch to files
- Checking if a patch can be applied without making changes
- Previewing the impact of a patch before applying
- Applying a patch while ignoring whitespace changes
- Reversing the application of a previously applied patch
- Stripping leading path components from patch files
- Being verbose during the patch application process