How to fix error: patch does not apply in Git

GitINTERMEDIATEMEDIUMUpdated Dec 6, 2025· 337 views

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.

Quick fix

Fixes error: patch does not apply

git apply -v example.patch
  1. Use verbose mode to diagnose the issue
  2. Check the patch with --check before applying
  3. Try 3-way merge with --3way
  4. Apply partial changes with --reject
git — error
git apply -v example.patch
error: patch does not apply

What this error means

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.

Typical symptoms

  • git apply command exits with 'error: patch does not apply'
  • git am fails with 'Patch failed' or 'patch does not apply' messages
  • Patches that worked previously now fail after pulling new changes
  • Patches from external contributors or other repositories fail to apply
  • CI/CD pipelines fail when applying patch files during build process

Common causes

  1. The target file has been modified since the patch was created, causing context lines to no longer match
  2. Line endings differ between the patch file and target files (LF vs CRLF)
  3. Whitespace differences such as tabs vs spaces or trailing whitespace changes
  4. The patch was created from a different branch or commit than the current checkout
  5. File was renamed or moved, but the patch references the old path
  6. The patch was created with a different context size than Git expects (unified diff settings)

How to fix "error: patch does not apply"

1Use verbose mode to diagnose the issue

Run git apply with the -v (verbose) flag to see exactly which hunks are failing and why:

bash
git apply -v example.patch

This 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.

2Check the patch with --check before applying

Before attempting a real apply, use the --check flag to test if the patch would apply cleanly:

bash
git apply --check example.patch

This performs a dry run without modifying any files. If it reports errors, you know you need to use one of the recovery options below.

3Try 3-way merge with --3way

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):

bash
git apply --3way example.patch

If 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.

4Apply partial changes with --reject

If --3way doesn't work or isn't available, use --reject to apply whatever hunks succeed and save failed hunks to .rej files:

bash
git apply --reject --whitespace=fix example.patch

After 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

5Reduce context requirements with -C

Git by default requires 3 lines of context to match. If your files have minor differences, reducing this can help:

bash
git apply -C1 example.patch

The -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.

6Handle whitespace and line ending issues

If the patch failure is due to whitespace differences, try these options:

bash
# 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.patch

For line ending issues (LF vs CRLF), you may need to convert the patch file:

bash
# Convert CRLF to LF on Linux/Mac
sed -i 's/\r$//' example.patch

# Or use dos2unix if available
dos2unix example.patch
7Apply from the correct directory

Ensure you're running git apply from the repository root, not a subdirectory. Patches contain paths relative to the repository root:

bash
# Navigate to repository root
cd /path/to/your/repo

# Then apply the patch
git apply example.patch

If the patch was created with a different directory structure, you can strip path components:

bash
# Strip the first directory from paths (e.g., a/src/file.js becomes src/file.js)
git apply -p1 example.patch
8For git am failures, use skip or abort

When using git am (which applies patches as commits), you have additional recovery options:

bash
# 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 --continue

You can also use git am --3way initially to enable three-way merge for the entire patch series.

Advanced notes

### 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:

bash
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.

Frequently asked questions

What does "error: patch does not apply" mean?
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.
How do I fix "error: patch does not apply"?
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.
What causes "error: patch does not apply"?
The target file has been modified since the patch was created, causing context lines to no longer match Line endings differ between the patch file and target files (LF vs CRLF) Whitespace differences such as tabs vs spaces or trailing whitespace changes
Is the "error: patch does not apply" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading