How to fix fatal: corrupt patch at line 10 in Git
This error occurs when Git cannot parse a patch file due to formatting issues such as missing trailing newlines, stripped whitespace on empty context lines, incorrect hunk line counts, or Windows line endings. Fixing the patch file structure resolves the issue.
Quick fix
Fixes fatal: corrupt patch at line 10
# Check if file ends with newline
tail -c 1 your-patch.patch | od -c- Check for missing trailing newline
- Verify line endings are correct
- Restore stripped whitespace on empty context lines
- Verify hunk header line counts
# Check if file ends with newlinetail -c 1 your-patch.patch | od -cfatal: corrupt patch at line 10
What this error means
When Git reports `fatal: corrupt patch at line N`, it means the patch file has structural problems that prevent Git from parsing it correctly. The line number in the error message indicates where Git encountered the problem, but the actual issue may originate earlier in the file. Patches follow a strict format called unified diff format. Each change begins with a hunk header (lines starting with `@@`) that specifies line counts, followed by context lines (prefixed with a space), removed lines (prefixed with `-`), and added lines (prefixed with `+`). If any of these elements are malformed, Git cannot apply the patch. The most common causes are subtle formatting issues: editors that strip trailing whitespace remove the required space on empty context lines, missing newlines at the end of the patch file, or line ending mismatches between Windows (CRLF) and Unix (LF) systems.
Typical symptoms
- git apply fails with 'fatal: corrupt patch at line N' where N varies based on the issue location
- git am fails when applying patches received via email
- git-gui shows 'corrupt patch' error when staging individual lines or hunks
- Patches created on one operating system fail to apply on another
- Manually edited patches fail to apply even though they look correct
- CI/CD pipeline fails when applying generated patches
Common causes
- Missing newline at the end of the patch file
- Empty context lines have their leading space character stripped by editors with 'remove trailing whitespace' enabled
- Windows line endings (CRLF) in a patch file being applied on Unix/Linux
- Hunk header line counts do not match the actual number of lines in the hunk
- Patch file was corrupted during email transmission (quoted-printable encoding issues)
- Copy-paste from web pages introduced encoding artifacts or stripped characters
- Source file lacks trailing newline and git-gui cannot stage partial changes
How to fix "fatal: corrupt patch at line 10"
The most common cause is a missing newline at the end of the patch file. Check if the file ends with a newline:
# Check if file ends with newline
tail -c 1 your-patch.patch | od -cIf you see 0000000 with no \n, add a newline:
# Add trailing newline
echo '' >> your-patch.patchAlternatively, open the file in your editor and ensure there's a blank line at the end.
Check if the patch file has Windows line endings (CRLF):
# Check for CRLF line endings
file your-patch.patch
# Or look for ^M characters
cat -A your-patch.patch | head -20If you see 'CRLF' or ^M at line ends, convert to Unix format:
# Using dos2unix
dos2unix your-patch.patch
# Or using sed
sed -i 's/\r$//' your-patch.patch
# In vim
:set ff=unix
:wqIf your editor removes trailing whitespace, empty context lines in the patch lose their required leading space. Look for lines that should have a single space but are completely empty:
# Show line contents with visible characters
cat -A your-patch.patchContext lines should start with a space character. If you see two consecutive $ markers (empty lines where there should be a space), you need to restore the spaces.
Fix in your editor: Disable 'remove trailing whitespace' setting when editing patches, or manually add a space to empty context lines.
Fix with sed (add space to empty lines between hunk header and next hunk/end):
sed -i '/^$/s/^/ /' your-patch.patchNote: This adds a space to ALL empty lines, which may not be correct. Manual editing is safer.
If you've manually edited the patch, the line counts in hunk headers may be wrong. Hunk headers look like:
@@ -32,9 +54,15 @@This means:
- -32,9: Starting at line 32 in the original file, show 9 lines
- +54,15: Starting at line 54 in the new file, show 15 lines
The 'before' count (9) should equal: context lines + removed lines (- prefix)
The 'after' count (15) should equal: context lines + added lines (+ prefix)
Count the lines manually and correct the header if needed. When count is 1, it can be omitted (e.g., -32 instead of -32,1).
Use Git's built-in validation to check the patch without applying it:
# Check if patch is valid
git apply --check your-patch.patch
# Show what would be applied (verbose)
git apply --stat your-patch.patchFor patches meant to be applied with git am:
# Dry run for git am
git am --dry-run your-patch.patchThis helps identify the exact line causing issues without modifying your working directory.
If you have access to the original commits, regenerating the patch is often easier than fixing a corrupted one:
# Generate patch from a single commit
git format-patch -1 <commit-hash>
# Generate patch from a range of commits
git format-patch <base-commit>..<head-commit>
# Generate diff (simpler format, but less metadata)
git diff <base-commit> <head-commit> > changes.patchgit format-patch creates patches with proper formatting and metadata that are less prone to corruption than raw diff output.
Patches received via email may have encoding issues. Common problems include quoted-printable encoding and wrapped long lines.
For git am with email patches:
# Keep CR characters if patch was created on Windows
git am --keep-cr your-patch.patch
# Specify transfer encoding (Git 2.3.0+)
git am --transfer-encoding=base64 your-patch.patchIf the patch was corrupted by email, ask the sender to:
1. Use git send-email instead of attaching patches
2. Or compress the patch before attaching: gzip your-patch.patch
If you get 'corrupt patch' in git-gui when staging individual lines, the issue is usually that your file doesn't end with a newline.
Solution: Add a trailing newline to your source file (not the patch):
# Check if file ends with newline
tail -c 1 your-file.txt | od -c
# Add newline if missing
echo '' >> your-file.txtAlternatively, stage the entire file instead of individual lines:
git add your-file.txtThis is a known limitation in git-gui when handling files without trailing newlines.
Advanced notes
Understanding unified diff format: The unified diff format has strict requirements. Each hunk begins with @@ -l,s +l,s @@ where l is the starting line and s is the number of lines. Context lines must start with exactly one space character. Missing or extra characters corrupt the patch.
Why empty context lines need a space: In unified diff, a line that's unchanged between versions is called a context line and must be prefixed with a single space. An empty line in the original file becomes a line containing only a space in the diff. Editors that strip trailing whitespace remove this space, corrupting the patch.
Patch validation tools: Besides git apply --check, you can use patch --dry-run < file.patch to test patches. The GNU patch command sometimes gives more descriptive error messages.
Binary patches: If your patch includes binary files, ensure they're properly encoded. Use git format-patch --binary when creating patches that include binary content.
Large patches: For very large patches, consider splitting them into smaller pieces using git format-patch with commit ranges. This makes debugging easier and reduces the chance of corruption during transmission.
Frequently asked questions
- What does "fatal: corrupt patch at line 10" mean?
- When Git reports fatal: corrupt patch at line N, it means the patch file has structural problems that prevent Git from parsing it correctly. The line number in the error message indicates where Git encountered the problem, but the actual issue may originate earlier in the file. Patches follow a strict format called unified diff format.
- How do I fix "fatal: corrupt patch at line 10"?
- This error occurs when Git cannot parse a patch file due to formatting issues such as missing trailing newlines, stripped whitespace on empty context lines, incorrect hunk line counts, or Windows line endings. Fixing the patch file structure resolves the issue.
- What causes "fatal: corrupt patch at line 10"?
- Missing newline at the end of the patch file Empty context lines have their leading space character stripped by editors with 'remove trailing whitespace' enabled Windows line endings (CRLF) in a patch file being applied on Unix/Linux
- Is the "fatal: corrupt patch at line 10" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Git Official Documentation
- ๐ฌStack Overflow - Git error: fatal: corrupt patch at line 36(Stack Overflow)
- ๐ฌStack Overflow - What causes git-apply corrupt patch?(Stack Overflow)
- ๐ฌ
- ๐Git Documentation - git-apply(Official Docs)
- ๐Git Documentation - git-format-patch(Official Docs)
- ๐Git Documentation - diff format(Official Docs)
- ๐ฌ
- ๐ฌ
- ๐
- ๐