This error occurs when Git cannot cleanly apply a patch file to your working directory because the file contents have changed since the patch was created. The fix involves using --3way merge, --reject flags, or adjusting context matching options.
The "error: patch does not apply" message indicates that Git's `git apply` or `git am` command cannot match the context lines in your patch file with the current state of your files. Patches work by finding specific lines of code (context) and then making changes relative to those lines. When the surrounding code has changed—whether through new commits, different formatting, or divergent branches—Git cannot locate where to apply the changes. This error is common when applying patches generated from a different branch or an older version of the codebase. It also occurs frequently when sharing patches between team members whose local repositories have diverged, or when applying community-contributed patches to projects that have evolved since the patch was created. Unlike merge conflicts during normal Git operations, patch application failures require manual intervention to identify what changed and how to reconcile the differences. Git provides several flags (--3way, --reject, -C) to help resolve these situations with varying levels of automation.
Run git apply with the -v (verbose) flag to see exactly which hunks are failing and why:
git apply -v example.patchThis will show you line-by-line details about what Git is attempting to match, helping you identify whether it's a context mismatch, whitespace issue, or something else.
Before attempting a real apply, use the --check flag to test if the patch would apply cleanly:
git apply --check example.patchThis performs a dry run without modifying any files. If it reports errors, you know you need to use one of the recovery options below.
The --3way option tells Git to use a three-way merge when the patch doesn't apply cleanly. This works if the patch contains blob SHA information (most patches created with git format-patch do):
git apply --3way example.patchIf conflicts occur, Git will leave conflict markers in the files (just like a merge conflict), which you can resolve manually and then stage the changes.
Note: The --3way option is incompatible with --reject. Choose one approach or the other.
If --3way doesn't work or isn't available, use --reject to apply whatever hunks succeed and save failed hunks to .rej files:
git apply --reject --whitespace=fix example.patchAfter running this:
1. Successfully applied changes are in your working directory
2. Failed hunks are saved in files like filename.rej
3. Manually review each .rej file and apply those changes by hand
4. Delete the .rej files when done
Git by default requires 3 lines of context to match. If your files have minor differences, reducing this can help:
git apply -C1 example.patchThe -C1 option tells Git to only require 1 line of context instead of 3. Use with caution—this increases the risk of applying changes in the wrong location.
If the patch failure is due to whitespace differences, try these options:
# Ignore whitespace changes during matching
git apply --ignore-whitespace example.patch
# Ignore changes in whitespace at EOL
git apply --ignore-space-change example.patch
# Fix whitespace errors according to core.whitespace
git apply --whitespace=fix example.patchFor line ending issues (LF vs CRLF), you may need to convert the patch file:
# Convert CRLF to LF on Linux/Mac
sed -i 's/\r$//' example.patch
# Or use dos2unix if available
dos2unix example.patchEnsure you're running git apply from the repository root, not a subdirectory. Patches contain paths relative to the repository root:
# Navigate to repository root
cd /path/to/your/repo
# Then apply the patch
git apply example.patchIf the patch was created with a different directory structure, you can strip path components:
# Strip the first directory from paths (e.g., a/src/file.js becomes src/file.js)
git apply -p1 example.patchWhen using git am (which applies patches as commits), you have additional recovery options:
# Skip the problematic patch and continue with the next one
git am --skip
# Abort the entire patch series and restore your branch
git am --abort
# After manually resolving conflicts, continue applying
git am --continueYou can also use git am --3way initially to enable three-way merge for the entire patch series.
### Understanding Three-Way Merge vs Reject
The --3way and --reject options use fundamentally different algorithms and are mutually exclusive:
- --3way: Requires that the patch includes blob SHA identifiers (from git format-patch). Git finds the original file version in your repository, applies the patch to it, then does a three-way merge with your current file. This enables proper conflict markers and is more intelligent about complex changes.
- --reject: Works with any patch format. Git simply tries each hunk independently, applying what works and writing failures to .rej files. This is more manual but works when --3way cannot find the base blobs.
### Fuzzy Matching with -C
The -C<n> option controls how many lines of context must match. Default is -C3. Lower values make matching more permissive:
- -C3 (default): All 3 context lines must match exactly
- -C1: Only 1 context line must match
- -C0: No context matching (dangerous—may apply in wrong location)
### Patches Without Context
If applying patches created with --unified=0 (no context lines), you need:
git apply --unidiff-zero example.patch### Cherry-pick vs Patches
If you have access to the original repository, git cherry-pick is often more reliable than patches because it:
1. Performs full rename detection
2. Uses proper three-way merge from the start
3. Handles binary files correctly
Consider using git cherry-pick when copying commits within the same repository or between repositories you have access to.
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