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.
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
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:
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:
Fix 3: Ignore Whitespace Differences
Whitespace changes (trailing spaces, tab vs spaces, line endings) can cause patch failures:
Fix 4: Adjust Fuzz Factor
Allow context lines to be slightly off (shifted up or down):
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
Using git am Instead of git apply
git am (apply mailbox) works with git format-patch output and supports three-way merge natively:
Verifying a Patch Before Applying
Creating Clean Patches
Avoid the error by creating patches that apply cleanly:
Common Pitfalls
- Patch created against a different branch or commit: If the patch was generated on
mainbut you are applying it tofeature, the surrounding context lines may not match. Check the base withgit apply --checkand either rebase to the correct base or use--3wayto let Git resolve differences. - Line ending differences (CRLF vs LF): Windows and Unix line endings cause context mismatches. Use
git apply --ignore-whitespaceor configurecore.autocrlfconsistently across environments. Patches generated on Windows may fail on Linux and vice versa. - Using
git applywithformat-patchoutput:git format-patchproduces patches with commit metadata (author, date, message).git applycan handle them butgit amis the correct tool — it preserves the commit metadata. Usegit am --3wayfor the best experience. - Applying a patch to the wrong directory level:
git diffcreates paths likea/src/file.pyandb/src/file.py. The-p1strip level (default forgit apply) removes thea/orb/prefix. If you generated the patch with a custom--prefix, you may needgit apply -p0or-p2to match. - Patch includes deleted files that were already deleted: If the patch deletes a file that no longer exists in your working tree,
git applyfails. Usegit apply --rejectto 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 --3wayfor the best conflict resolution (falls back to merge) - Use
git apply --rejectto apply clean hunks and manually fix rejected ones - Use
git apply --ignore-whitespacefor whitespace-related failures - Prefer
git am --3wayovergit applyfor patches fromgit format-patch— it preserves commit metadata
Related reading
- git archive fatal Operation not supported by protocol
- Git as mercurial client? Why no git-hg?
- Git asks for username every time I push
- Git asks for username every time I push
- Git Bash is extremely slow on Windows 7 x64
- Git Bash won't run my python files?
- Git branch command behaves like 'less
- Git branch command behaves like 'less
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.