"error: ... patch does not apply" when editing hunk with "git add -p"

"error: ... patch does not apply" when editing hunk with "git add -p"

tldr:为什么我在编辑大块并删除一行时得到 error: ... patch does not apply,即使它看起来应该有效?

我在做 git add -p 时正在编辑大块头,我只想添加 new line 1(而不是添加 new line 2):

# Manual hunk edit mode -- see bottom for a quick guide
@@ -1 +1,3 @@
 first line
+new line 1
+new line 2
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.

我将 + 更改为 space new line 2:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -1 +1,3 @@
 first line
+new line 1
 new line 2
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.

但这给出了错误:

error: patch failed: myfile:1
error: myfile: patch does not apply
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]? 

有什么问题?

改为删除该行。

+ 更改为 space 不是 正确的操作。
根据 git (# To remove '+' lines, delete them. ) 给出的指示,需要的是 delete new line 2:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -1 +1,3 @@
 first line
+new line 1
# (^^^ note that "new line 2" has been deleted ^^^)
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.