2015-11-03 153 views
3

当使用--fixup和--autosquash时,git给了我很大的头痛。 我想举两个例子,一个工作得很好,另一个是一团糟。 (GIT版本2.6.2)git rebase -i -autosquash冲突

工作例如:

首先承诺:

$ git init 
$ printf '1\n' > test.file 
$ git add test.file 
$ git commit -m 'Insert 1 --> first line' 
$ cat test.file 
1 

第二次提交(BUG):

$ printf 'This is\na BUG\n' >> test.file 
$ git commit -am 'Added line 2 and 3 with BUG' 
$ cat test.file 
1 
This is 
a BUG 

三犯:

$ sed -i '2i 2' test.file 
$ git commit -am 'Insert 2 --> second line' 
$ cat test.file 
1 
2 
This is 
a BUG 

第四提交(修正):

$ sed -i 's/a BUG/NOT a BUG/' test.file 
$ git add test.file 
$ git log --oneline 
b021696 Insert 2 --> second line 
2e18b8d Added line 2 and 3 with BUG 
d7b60a1 Insert 1 --> first line 
$ git commit --fixup HEAD~ 
$ cat test.file 
1 
2 
This is 
NOT a BUG 

衍合:

$ git log --oneline 
fe99989 fixup! Added line 2 and 3 with BUG 
b021696 Insert 2 --> second line 
2e18b8d Added line 2 and 3 with BUG 
d7b60a1 Insert 1 --> first line 

$ git rebase -i --autosquash HEAD~3 

[detached HEAD 6660b0e] Added line 2 and 3 with BUG 
Date: Tue Nov 3 13:28:07 2015 +0100 
1 file changed, 2 insertions(+) 
Successfully rebased and updated refs/heads/master. 

头痛例如:(唯一的区别是BUGGY提交我是个单线)

首先提交:

$ git init 
$ printf '1\n' > test.file 
$ git add test.file 
$ git commit -m 'Insert 1 --> first line' 
$ cat test.file 
1 

二提交(BUG):

$ printf 'This is a BUG\n' >> test.file 
$ git commit -am 'Added line 2 with BUG' 
$ cat test.file 
1 
This is a BUG 

第三提交:

$ sed -i '2i 2' test.file 
$ git commit -am 'Insert 2 --> second line' 
$ cat test.file 
1 
2 
This is a BUG 

四犯(修正):

$ sed -i 's/a BUG/NOT a BUG/' test.file 
$ git add test.file 
$ git log --oneline 
2b83fe7 Insert 2 --> second line 
62cdd05 Added line 2 with BUG 
0ee3343 Insert 1 --> first line 
$ git commit --fixup HEAD~ 
$ cat test.file 
1 
2 
This is NOT a BUG 

再次基于:

$ git log --oneline 
c3d3db7 fixup! Added line 2 with BUG 
2b83fe7 Insert 2 --> second line 
62cdd05 Added line 2 with BUG 
0ee3343 Insert 1 --> first line 
$ git rebase -i --autosquash HEAD~3 
error: could not apply c3d3db7... fixup! Added line 2 with BUG 

When you have resolved this problem, run "git rebase --continue". 
If you prefer to skip this patch, run "git rebase --skip" instead. 
To check out the original branch and stop rebasing, run "git rebase --abort". 

Could not apply c3d3db78440e48c1bb637f78e0767520db65ea1e... fixup! Added line 2 with BUG 

$ git status 
interactive rebase in progress; onto 0ee3343 
Last commands done (2 commands done): 
    pick 62cdd05 Added line 2 with BUG 
    fixup c3d3db7 fixup! Added line 2 with BUG 
Next command to do (1 remaining command): 
    pick 2b83fe7 Insert 2 --> second line 
    (use "git rebase --edit-todo" to view and edit) 
You are currently rebasing branch 'master' on '0ee3343'. 
    (fix conflicts and then run "git rebase --continue") 
    (use "git rebase --skip" to skip this patch) 
    (use "git rebase --abort" to check out the original branch) 

Unmerged paths: 
    (use "git reset HEAD <file>..." to unstage) 
    (use "git add <file>..." to mark resolution) 

     both modified: test.file 

no changes added to commit (use "git add" and/or "git commit -a") 

$ cat test.file 
1 
<<<<<<< HEAD 
This is a BUG 
======= 
2 
This is NOT a BUG 
>>>>>>> c3d3db7... fixup! Added line 2 with BUG 

为什么修正不干净的应用?

为什么fixup还包含“2”,它不应该在修正引入的修补程序中,而是包含在前一个提交的修补程序中。

+0

是'<<<<<<< HEAD这是一个错误,它真的会被两行分割:'<<<<<<< HEAD'和'This is a BUG'? –

+0

是的。对不起,并感谢您编辑:) – JohnDuhh

+0

好的。我想我知道问题是什么。让我看看是否使用不同的合并方法可以解决它。 –

回答

2

当您执行--fixup时,您正在按顺序应用修补程序,因此上下文已消失。在第一种情况下,你的补丁应用如下:

  1. 插入1在第1行
  2. 插入This is\naBUG上线2,3后1
  3. 删除线a BUG,在第4行,This is后,更换与NOT a BUG
  4. 插入2第2行1后,This is
之前

步骤2,3相当明确。即使行号与步骤3中预期的行号不同,上下文也会说明清楚。在第二种情况下,

  1. 插入1在第1行
  2. 1
  3. 删除线This is a BUG

  4. 插入This is a BUG第2行,第3行与This is NOT a BUG替换行之后2
  5. 插入2第2行,之后1,之前This is a BUG

在这种情况下,修补程序#3是不可能的,因为This is a BUG未出现在第3行,而其之前的行不是2。在这种情况下,Git并不认为第2行是正确的,因为缺少上下文。

解决此问题的最简单方法是重新设置底座的顺序以反映您实际正在做的事情。

pick 5ef0459 Added line 2 with BUG 
fixup ed5cd81 fixup! Added line 2 with BUG 
pick 20e104e Insert 2 --> second line 

开关的最后两个元素给该补丁需要上下文:代替原有的秩序

pick 5ef0459 Added line 2 with BUG 
pick 20e104e Insert 2 --> second line 
fixup ed5cd81 fixup! Added line 2 with BUG 

在这种情况下,你可能需要将-k标志添加到您的命令行保存最后一次提交,这基本上是空的:

$ git rebase -i -k --autosquash HEAD~3 
Date: Tue Nov 3 10:45:40 2015 -0500 
1 file changed, 2 insertions(+), 1 deletion(-) 
Successfully rebased and updated refs/heads/master. 
$ cat test 
1 
2 
This is NOT a BUG 

另一种方法是当然的固定冲突使用git merge或手动,在rebase失败时按照提示进行操作。

您可以通过添加-s recursive -X theirs-s recursive -X ours来指定策略,使rebase“成功”。但是,由于上下文冲突,在这两种情况下,您的修正都会遭到破坏。

+0

呼叫良好。固定的 –

+0

删除行'这是一个BUG',在'2'后面的第4行替换为'This is not a BUG' 应该是 - > 删除行'这是一个BUG',替换为'This is NOT线路'2'后的'3'线上的BUG'? – JohnDuhh

+0

'3'应该是3 :) –

3

我认为问题是变化的背景。看看这个承诺:

$ git show c3d3db7 
diff --git a/test.file b/test.file 
index 7a103db..8c8e69a 100644 
--- a/test.file 
+++ b/test.file 
@@ -1,3 +1,3 @@ 
1 
2 
-This is a BUG 
+This is NOT a BUG 

而且要将此补丁应用到文件与内容:

1 
This is a BUG 

看到了吗?该补丁不适用,因为上下文不匹配。所以出现冲突,你必须手动修复它。


当你有开溜线一分为二,补丁是一样的东西:

diff --git a/test.file b/test.file 
--- a/test.file 
+++ b/test.file 
@@ -1,3 +1,3 @@ 
1 
2 
This is 
-a BUG 
+NOT a BUG 

和文件是:

1 
This is 
a BUG 

现在,虽然比赛并不完美,至少上下文的第一条未修改的行匹配,因此合并可能会继续。