git 暂存单行时“致命:第 XX 行损坏的补丁”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12147930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:27:33  来源:igfitidea点击:

"fatal: corrupt patch at line XX" when staging single line

gitgit-gui

提问by Kirill Kulakov

I'm getting the following error when I'm trying to stage a single line or multiple lines using the git gui (right click -> stage lines for commit). It's not the first time it occure to me, and I've found others facing it.However I couldn't find how to solve it.

当我尝试使用 git gui 暂存单行或多行时出现以下错误(右键单击 -> 暂存行以进行提交)。这不是我第一次遇到它,我发现其他人也遇到了它。但是我找不到如何解决它。

Did any one ever encountered this problem? is there something I can do (staging all the file is not a real solution)

有没有人遇到过这个问题?有什么我可以做的吗(暂存所有文件不是真正的解决方案)

Update:Here is a file which gives me the following error when I try to stage the deleted line.

更新:这是一个文件,当我尝试暂存已删除的行时,它给了我以下错误。

@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/texture"
    android:tileMode="repeat"
-   android:dither="true"
    >
</bitmap>
\ No newline at end of file

Here is the error message:

这是错误消息:

fatal: corrupt patch at line 14

strangely the following fine doesn't even have 14 line!? note ending file with a new line didn't solve the problem

奇怪的是下面的罚款甚至没有14行!?注意用新行结束文件没有解决问题

回答by Kyle

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

显然,Git GUI要求文件在暂存单个行时以换行符结尾

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.

我很确定至少在某个时间点,即使文件末尾没有换行符,也可以暂存各行,但显然这不再可能。我自己就遇到了这个问题,文件末尾的换行符修复了它,删除它们会导致它。

回答by 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=fixas workaround but this is another thing you must not mix into.

我看到有些人将其--ignore-space-change--ignore-whitespace--whitespace=fix用作解决方法,但这是另一件您不能混入的事情。

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

打开您的补丁并检查您要保留的所有行是否以“ ”(空格)开头

UPD

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

UPD2

So if in your patch in next line

所以如果在下一行的补丁中

android:tileMode="repeat"

Your editor remove trainling space. Patch will FAIL

您的编辑器删除训练空间。补丁将失败

回答by Zonko

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

当您使用的块太大(即修改了太多连续行)时,Git GUI 无法从索引中添加/删除行。

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

我的解决方法:如果更改不是“粉红色墙”(如果块中间有一些未修改的行),请转到编辑 > 选项并减少差异上下文中的行数。如果这不起作用,你就完蛋了;使用其他工具(例如命令行)。

回答by Mike B

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.

这里提到的“空白”提醒我,在我的 git diff 选项中,我有“-w”,它在比较时跳过空白。

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

我删除了那个标志(使用 git-gui 的“选项...”),并且能够在没有错误的情况下上演大块头。然后我将它添加回来(因为我通常不想在检查更改的内容时看到空白差异)。

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

不确定这对您有帮助,但尝试并易于撤消是无害的。

回答by stanri

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.

我遇到了这个问题,因为我试图在 emacs 中为我的补丁添加一个换行符,当我保存它时,它会再次自动删除最后的换行符。

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.

如果您使用 emacs 在补丁的末尾添加换行符,它也可能会再次删除它。我打开了另一个文本编辑器来添加我的换行符,它起作用了。

回答by xmoex

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

受到 kyl191 的回答的启发,我想出了一个简单的解决方案来解决这个问题:

  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
    
  1. 在文件末尾添加换行符

  2. 隐藏所有更改

    git stash
    
  3. 再次在文件末尾添加换行符,这样您当前文件和隐藏版本中都有一个换行符

  4. 上演相应的大块头(仅包含您的换行符)

    git stage <your file>
    
  5. 从 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.

现在您应该能够通过 gui 暂存单行而不会出现此错误。这是有效的,因为在您仅暂存包含换行符的部分之后,与从存储恢复的版本的差异已经看到一个以换行符结尾的文件。

回答by Madacol

Yet another workaround

另一个解决方法

Stagewhole file. Unselectlines

暂存整个文件。取消选择