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