The 'patch does not apply' error occurs when Git cannot match the context lines in a patch file to your current codebase. This commonly happens when the patch has already been applied, the code has changed since the patch was created, or there are whitespace differences.
The "patch failed: patch does not apply" error in Git indicates that the `git apply` command cannot find the expected context in your files to apply the changes specified in the patch. When Git applies a patch, it doesn't just look at the lines being changed - it also verifies the surrounding context lines (typically 3 lines before and after each change) match exactly. If those context lines don't match what's in the patch file, Git refuses to apply the patch to prevent corrupting your code. The most common reason for this error is that the patch has already been applied to your codebase. When changes are already present, the "before" state in the patch doesn't match your current "after" state. Other causes include code modifications since the patch was created, whitespace differences (tabs vs spaces, line endings), or applying the patch from the wrong directory.
Use the --reverse --check flags to detect if the patch was already applied:
# If this succeeds, the patch is already applied
git apply --reverse --check my-patch.patch
# If the regular check fails but reverse check succeeds:
git apply --check my-patch.patch # This will fail
git apply --reverse --check my-patch.patch # This will succeedIf the reverse check succeeds, your changes are already in the codebase and you can skip applying the patch.
Add the -v flag to see exactly where the patch fails:
git apply -v my-patch.patchThis shows which hunks are being applied and where the context mismatch occurs. Look for lines like:
Checking patch file.txt...
error: while searching for:
expected context line 1
expected context line 2
error: patch failed: file.txt:10Compare the "expected" context with what's actually in your file at that line number.
If the patch contains blob identifiers, Git can attempt a three-way merge:
git apply --3way my-patch.patchThis option:
- Falls back to 3-way merge when direct application fails
- May leave conflict markers in files for manual resolution
- Requires the original blobs to be available in your repository
- Is incompatible with --reject and --cached options
After running, check for conflict markers (<<<<<<<, =======, >>>>>>>) in affected files.
Apply the parts that work and save rejected hunks for manual review:
git apply --reject my-patch.patchThis creates .rej files for hunks that couldn't be applied:
# Check for rejection files
find . -name "*.rej"
# Review a rejection file
cat file.txt.rejManually apply the rejected changes by editing the original files, then delete the .rej files when done.
Whitespace differences are a common cause. Try these options:
# Ignore whitespace when matching context
git apply --ignore-whitespace my-patch.patch
# Also ignore changes in whitespace amount
git apply --ignore-space-change my-patch.patch
# Fix whitespace issues according to core.whitespace
git apply --whitespace=fix my-patch.patchFor patches created on Windows but applied on Linux (or vice versa), line ending differences may cause issues:
# Convert patch line endings to Unix format
sed -i 's/\r$//' my-patch.patchPatches are relative to the directory where they were created. Ensure you're applying from the right location:
# Check the paths in the patch file
head -20 my-patch.patch
# You might see paths like:
# --- a/src/file.txt
# +++ b/src/file.txt
# Make sure you're at the repository root
cd $(git rev-parse --show-toplevel)
git apply my-patch.patchIf the patch was created in a subdirectory, you may need the -p option to strip path components:
# Strip 1 leading path component (removes a/ and b/)
git apply -p1 my-patch.patch
# Strip 2 leading components
git apply -p2 my-patch.patchIf you have access to the original source, create a new patch with more context lines to improve matching:
# Generate patch with 10 lines of context instead of default 3
git diff -U10 commit1 commit2 > patch-with-context.patch
# Or generate with maximum context
git diff -U999 commit1 commit2 > full-context.patch
# Apply the new patch
git apply patch-with-context.patchMore context helps Git find the correct location even if nearby code has changed.
If the patch was generated with git format-patch, use git am instead:
# Apply email-formatted patch
git am my-patch.patch
# If it fails, you can skip or abort
git am --skip # Skip this patch and continue with next
git am --abort # Abort the entire operation
# Use 3-way merge with git am
git am --3way my-patch.patchgit am provides better handling for patch series and preserves commit metadata.
### Detecting Already-Applied Patches Programmatically
You can create a script to check if a patch is already applied:
#!/bin/bash
PATCH="$1"
if git apply --reverse --check "$PATCH" 2>/dev/null; then
echo "Patch already applied"
exit 0
elif git apply --check "$PATCH" 2>/dev/null; then
echo "Patch can be applied"
git apply "$PATCH"
else
echo "Patch cannot be applied - manual intervention needed"
exit 1
fi### Understanding Patch Context
Git patches include context lines to ensure changes are applied to the correct location. The default is 3 lines:
@@ -8,7 +8,7 @@ function example() {
// context line 1
// context line 2
// context line 3
- old line to remove
+ new line to add
// context line 4
// context line 5
// context line 6If any context line doesn't match, the patch fails. When code has been refactored or lines have moved, the context may no longer match even if the actual change location still exists.
### The --3way vs --reject Trade-off
- --3way: Better for patches where you have the original blobs; creates standard Git conflict markers
- --reject: Better for patches from external sources; creates .rej files for manual review
These options are mutually exclusive - you cannot use both at once.
### Fuzz Factor (Not Available in Git)
Unlike the traditional patch command, git apply does not support a fuzz factor to allow inexact matching. This is by design to prevent applying changes to the wrong location. If you need fuzz matching, you can use the system patch command:
patch -p1 --fuzz=3 < my-patch.patchHowever, be careful as this can apply changes to unintended locations.
### Handling Binary Files in Patches
Binary files in patches require the --binary flag and the original blobs must be available:
git apply --binary my-patch.patchIf binary patches fail, you may need to handle those files manually.
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git