6

我正在尝试将更改拆分为多个提交,但是在手动编辑大块时遇到了问题。

原始大佬:

@@ -116,8 +116,8 @@
        context
        context
-           remove 1
-           remove 2
-           remove 3
+           add 1
+           add 2
+           add 3
        context
        context
        context

我只想上演发生在“删除 1”和“删除 2”的更改。换句话说,我需要从提交中排除“删除 3”。

我试过这个:

@@ -116,4 +116,4 @@
            context
            context
-           remove 1
-           remove 2
+           add 1
+           add 2

但它一直输出补丁不适用。我只删除了最后的上下文行和“删除 3”和“添加 3”行。我编辑了大块范围并减去了 4 个排除行(3 个是上下文,1 个是更改,1 个被删除,1 个被添加)

我使用了 2 个不同的编辑器,“nano”和“sublime text”,它们的结果相同。我确保没有没有被注释掉的空行。

我究竟做错了什么?

4

2 回答 2

0

我究竟做错了什么?

好吧,您正在手动编辑补丁文件,这似乎是一件奇怪的事情......

据我所知,git需要补丁中的尾随上下文。例如,如果我从一个看起来像这样的文件开始:

the
quick
brown
fox
jumped
over
the
lazy
dog

我有一个这样的补丁:

diff --git a/file1 b/file1
index 4a3cebe..30f5937 100644
--- a/file1
+++ b/file1
@@ -1,9 +1,9 @@
 the
 quick
 brown
-fox
-jumped
-over
+ostrich
+shouted
+at
 the
 lazy
 dog

这毫无问题地适用:

$ git apply mypatch

如果我删除该补丁中的尾随上下文(并更新行号),给我这个:

diff --git a/file1 b/file1
index 4a3cebe..30f5937 100644
--- a/file1
+++ b/file1
@@ -1,6 +1,6 @@
 the
 quick
 brown
-fox
-jumped
-over
+ostrich
+shouted
+at

然后git会拒绝应用补丁:

$ git apply diff
error: patch failed: file1:1
error: file1: patch does not apply

我什至添加了一行尾随上下文,它将起作用:

diff --git a/file1 b/file1
index 4a3cebe..30f5937 100644
--- a/file1
+++ b/file1
@@ -1,7 +1,7 @@
 the
 quick
 brown
-fox
-jumped
-over
+ostrich
+shouted
+at
 the
于 2015-03-18T20:03:13.380 回答
0

当 git 应用补丁时,它会查看前导和尾随上下文行。当 hunk 中没有前导上下文行时,hunk 必须应用在前映像的开头(更改之前的文件版本)。同样,没有尾随上下文意味着大块被锚定在最后。

Since you've removed the trailing context lines (and the hunk is not supposed to be achored at the end), the patch won't apply.

于 2015-03-31T11:13:07.677 回答