“fatal: corrupt patch at line XX” when staging single line

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 04:41:19

Apparently Git GUI requires that files end with a newline when staging individual lines.

I'm pretty sure that at at least one point in time, staging individual lines was ok even without a newline at the end of the file, but apparently that's no longer possible. Just ran into this problem myself, having newlines at the end of the file fixes it, and removing them causes it.

Eugen Konkov

Actually, this often happens when you edit '-' lines.
When you remove '-' and forget to add ' ' (space) instead of it

Or, by mistake, you add two spaces and you use 'tabs' as identation

Open your patch and check that all lines you want to leave untouched are started with ' ' (one space)

I have saw that some people use --ignore-space-change --ignore-whitespace --whitespace=fix as workaround but this is another thing you must not mix into.

Open your patch and check that all lines you want to leave untouched are started with ' ' (space)

UPD

it also possible your editor has option: "Delete spaces at end line" So, when you in your editor save patch:

-Line with space at end <--- NOTICE: Here one space at the end
+Line with no space at end<--- Here no space

Your editor 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 origin file has no line:

-Line with space at end<---

it has:

-Line with space at end <--- 

UPD2

So if in your patch in next line

android:tileMode="repeat"

Your editor remove trainling space. Patch will FAIL

Git GUI fails to add/remove lines from the index when the chunk you are working with is too big (i.e. too many successive lines modified).

My workaround : if the changes are not a "wall of pink" (if there are some unmodified lines in the middle of the chunk), go to edit > options and reduce the number of lines in the context of diffs. If this doesn't work, you are screwed ; use an other tool (like the command line for instance).

I was getting this error while staging hunks.

The "whitespace" mentioned here reminded me that I have, in my git diff options, "-w", which skips white-space while diffing.

I removed that flag (using git-gui's "Options..."), and was able to stage hunks without the error. I then added it back (because I usually don't want to see whitespace diffs when examining what has changed).

Not sure this will help you but it's rather harmless to try and easy to undo.

Inspired by kyl191's answer I came up with a simple solution for this problem:

  1. add newline character to the end of your file

  2. stash all changes

    git stash
    
  3. add newline character to the end of your file once again, so you have a newline character in your current file as well as in your stashed version

  4. stage the corresponding hunk (contains just your newline)

    git stage <your file>
    
  5. pop your changes from stash

    git stash pop
    

Now you should be able to stage single lines via gui without this error. This works because after you staged just the newline containing part, the diff to the version recovered from stash already sees a file wich ends with a newline character.

I had this problem because I tried to add a newline to my patch in emacs and it automatically removed the newline at the end again when I saved it.

If you're using emacs to add a newline to the end of your patch, it may also be removing it again. I opened another text editor to add my newline and it worked.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!