2015-11-03 205 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 

現在,雖然比賽並不完美,至少上下文的第一條未修改的行匹配,因此合併可能會繼續。