【问题标题】:"error: ... patch does not apply" when editing hunk with "git add -p"使用“git add -p”编辑大块时“错误:...补丁不适用”
【发布时间】:2020-10-22 03:12:56
【问题描述】:

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.

我将+ 更改为空格 为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]? 

有什么问题?

【问题讨论】:

    标签: git git-add git-patch


    【解决方案1】:

    改为删除该行。

    将+ 更改为空格 是不是正确的操作。
    根据 git (# To remove '+' lines, delete them. ) 给出的指示,需要删除 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.
    

    【讨论】:

    • 你还必须编辑标题:我认为@@ -1 +1,3 @@应该变成@@ -1 +1,2 @@
    • @phd:Git 将重新计算行数,因此无需编辑 hunk-header。
    猜你喜欢
    • 2014-05-10
    • 2012-03-19
    • 2018-06-29
    • 2014-11-08
    • 2021-11-03
    • 2015-05-21
    • 2016-08-02
    • 2011-03-17
    • 2011-06-13
    相关资源
    最近更新 更多