I have a Java file that ends like this:
}
}
And I mistakenly erased the newline at the end some time ago, but it was fine just until today when I got an error message from Git-GUI when commiting
fatal: corrupt patch at line 36
I tried adding the missing newline, but Git seems not to be able to handle it right:
Before adding newline:
}
}
\ No newline at end of file
After adding newline:
}
-}
\ No newline at end of file
+}
And it still gives me that error.
I tried reverting changes and adding only the newline without other changes to the file, but it didn't help either.
EDIT: Adding two or even three newlines doesn't help too.
EDIT2: This error occurs only when commiting lines within the last hunk.
This is happens when you edit '-' lines.
When you remove '-' and forget to add ' ' (space) instead of it
Open your patch and check that all lines you want to leave untouched are started with ' ' (space)
UPDATE
It is also possible that your editor has the option: "Delete spaces at the end of line". So, when you save the patch in your editor:
-Line with space at end <--- NOTICE: Here there is one space at the end
+Line with no space at end<--- Here there's no space
Your editor will remove trailing space and patch become like this:
-Line with space at end<--- Here no space. Patch will FAIL!!!
+Line with no space at end<--- Here no space also
This patch will FAIL because the origin file has no line:
-Line with space at end<---
instead it has:
-Line with space at end <---
UPD
Sometimes you want to remove -
lines. You change it by whitespace which is trimmed. So this does not work (here is just wihitespace as first character):
To workaround this just add +
line after it:
-
+
This patch will remove empty line and add it again