git
patch
error
troubleshooting
git-apply

git apply fails with patch does not apply error

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The git apply error "patch does not apply" means the patch file does not match the current state of the files in your working tree. The context lines (unchanged lines surrounding the changes) in the patch do not match the actual file content, so Git cannot determine where to apply the changes. The most common causes are: the patch was generated against a different version of the code, the file has been modified since the patch was created, or there are whitespace differences. Fixes include using --3way for three-way merge, --reject to apply what you can and fix the rest manually, or rebasing to the correct base commit.

The Error

bash
$ git apply my-changes.patch
error: patch failed: src/main.py:42
error: src/main.py: patch does not apply

This means line 42 and its surrounding context in the patch do not match the current file content.

Fix 1: Three-Way Merge (--3way)

The most reliable fix — uses Git's merge machinery to resolve conflicts:

bash
git apply --3way my-changes.patch
# or shorthand
git apply -3 my-changes.patch

If there are conflicts, they appear as standard merge conflict markers (<<<<<<<, =======, >>>>>>>) that you can resolve manually. This requires the patch to have been generated with git format-patch (which includes commit info) or against a commit that exists in your repository.

Fix 2: Apply with Reject Files (--reject)

Apply the parts that match and save failures as .rej files:

bash
1git apply --reject my-changes.patch
2# Applies clean hunks, creates .rej files for failed hunks
3
4# Check which files have rejects
5find . -name "*.rej"
6# ./src/main.py.rej
7
8# View the rejected hunk
9cat src/main.py.rej
10# Shows the diff hunk that could not be applied
11
12# Manually apply the rejected changes
13# Then delete the .rej file
14rm src/main.py.rej

Fix 3: Ignore Whitespace Differences

Whitespace changes (trailing spaces, tab vs spaces, line endings) can cause patch failures:

bash
1# Ignore whitespace when applying
2git apply --ignore-whitespace my-changes.patch
3
4# Ignore specific whitespace changes
5git apply --ignore-space-change my-changes.patch

Fix 4: Adjust Fuzz Factor

Allow context lines to be slightly off (shifted up or down):

bash
# Allow patch to apply with shifted context (like GNU patch)
# git apply does not have a --fuzz option, use patch instead
patch -p1 --fuzz=3 < my-changes.patch

git apply is strict about context matching. The traditional patch command has a --fuzz option that allows approximate matching.

Fix 5: Check the Patch Against the Right Base

bash
1# Check which commit the patch was created against
2head -20 my-changes.patch
3# Look for "From <commit-hash>" or base file versions
4
5# Check if the patch applies cleanly (dry run)
6git apply --check my-changes.patch
7
8# Stash current changes first
9git stash
10git apply my-changes.patch
11git stash pop
12
13# Or checkout the correct base commit
14git log --oneline  # Find the right base commit
15git checkout <base-commit>
16git apply my-changes.patch
17git checkout -  # Go back to previous branch

Using git am Instead of git apply

git am (apply mailbox) works with git format-patch output and supports three-way merge natively:

bash
1# Generate a patch with full commit info
2git format-patch -1 HEAD
3# Creates 0001-commit-message.patch
4
5# Apply with git am (supports -3 for three-way merge)
6git am --3way 0001-commit-message.patch
7
8# If conflicts occur
9git am --abort    # Cancel and go back
10# or
11# Fix conflicts, then:
12git add .
13git am --continue
14
15# Apply a series of patches
16git am --3way *.patch

Verifying a Patch Before Applying

bash
1# Dry run — check without modifying files
2git apply --check my-changes.patch
3
4# Show statistics
5git apply --stat my-changes.patch
6# src/main.py | 15 ++++++++-------
7# src/utils.py |  8 ++++++++
8# 2 files changed, 16 insertions(+), 7 deletions(-)
9
10# Show what would be applied (verbose)
11git apply --verbose my-changes.patch
12
13# Reverse apply (undo a patch)
14git apply --reverse my-changes.patch

Creating Clean Patches

Avoid the error by creating patches that apply cleanly:

bash
1# Create patch from staged changes
2git diff --cached > staged-changes.patch
3
4# Create patch from last N commits (includes commit metadata)
5git format-patch -3  # Last 3 commits
6
7# Create patch between branches
8git diff main..feature-branch > feature.patch
9
10# Create patch with more context lines (reduces ambiguity)
11git diff -U10 > more-context.patch  # 10 context lines instead of 3
12
13# Binary-safe patches (includes images, etc.)
14git diff --binary > binary-safe.patch

Common Pitfalls

  • Patch created against a different branch or commit: If the patch was generated on main but you are applying it to feature, the surrounding context lines may not match. Check the base with git apply --check and either rebase to the correct base or use --3way to let Git resolve differences.
  • Line ending differences (CRLF vs LF): Windows and Unix line endings cause context mismatches. Use git apply --ignore-whitespace or configure core.autocrlf consistently across environments. Patches generated on Windows may fail on Linux and vice versa.
  • Using git apply with format-patch output: git format-patch produces patches with commit metadata (author, date, message). git apply can handle them but git am is the correct tool — it preserves the commit metadata. Use git am --3way for the best experience.
  • Applying a patch to the wrong directory level: git diff creates paths like a/src/file.py and b/src/file.py. The -p1 strip level (default for git apply) removes the a/ or b/ prefix. If you generated the patch with a custom --prefix, you may need git apply -p0 or -p2 to match.
  • Patch includes deleted files that were already deleted: If the patch deletes a file that no longer exists in your working tree, git apply fails. Use git apply --reject to skip those hunks, or manually remove the delete hunks from the patch file before applying.

Summary

  • "Patch does not apply" means the patch context does not match current file content
  • Use git apply --3way for the best conflict resolution (falls back to merge)
  • Use git apply --reject to apply clean hunks and manually fix rejected ones
  • Use git apply --ignore-whitespace for whitespace-related failures
  • Prefer git am --3way over git apply for patches from git format-patch — it preserves commit metadata

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.