git-rebase-i-autosquash冲突

git-rebase-i-autosquash冲突,git,conflict,fixup,Git,Conflict,Fixup,git在使用--fixup和--autosquash时让我非常头疼。 我想举两个例子,一个工作得很好,另一个一团糟。(git版本2.6.2) 工作示例: 第一次提交: 第二次提交(错误): 第三次承诺: 第四次提交(修正): 重新基准: 头痛的例子:(唯一的区别是错误的提交是一行) 第一次提交: 第二次提交(错误): 第三次承诺: 第四次提交(修正): 重新基准: $git日志--oneline c3d3db7修正!添加了带有BUG的第2行 2b83fe7插入2-->第二行 62cdd05添加

git在使用--fixup和--autosquash时让我非常头疼。 我想举两个例子,一个工作得很好,另一个一团糟。(git版本2.6.2)

工作示例:

第一次提交:

第二次提交(错误):

第三次承诺:

第四次提交(修正):

重新基准:


头痛的例子:(唯一的区别是错误的提交是一行)

第一次提交:

第二次提交(错误):

第三次承诺:

第四次提交(修正):

重新基准:

$git日志--oneline
c3d3db7修正!添加了带有BUG的第2行
2b83fe7插入2-->第二行
62cdd05添加了带有错误的第2行
0ee3343插入1-->第一行
$git rebase-i--自动取消头~3
错误:无法应用c3d3db7。。。修理!添加了带有BUG的第2行
解决此问题后,运行“git-rebase--continue”。
如果您希望跳过此修补程序,请运行“git-rebase--skip”。
要签出原始分支并停止重定基址,请运行“git-rebase--abort”。
无法应用c3d3db78440e48c1bb637f78e0767520db65ea1e。。。修理!添加了带有BUG的第2行
$git状态
正在进行的交互式重新基址;转到0ee3343
最后完成的命令(完成2个命令):
拾取62cdd05添加了带有错误的第2行
修正c3d3db7修正!添加了带有BUG的第2行
下一个要执行的命令(剩余1个命令):
选择2b83fe7插入2-->第二行
(使用“git rebase--编辑todo”查看和编辑)
您当前正在对“0ee3343”上的分支“主”重定基址。
(修复冲突,然后运行“git-rebase--continue”)
(使用“git-rebase--skip”跳过此修补程序)
(使用“git rebase--abort”签出原始分支)
未合并路径:
(使用“git重置磁头…”取消分级)
(使用“git add…”标记分辨率)
都已修改:test.file
未向提交添加任何更改(使用“git add”和/或“git commit-a”)
$cat test.file
1.
>c3d3db7。。。修理!添加了带有BUG的第2行

为什么修正不干净地应用


为什么修复程序还包含“2”,它不应该出现在修复程序引入的修补程序中,而应该出现在前一次提交的修补程序中。

我认为问题在于更改的上下文。看看这个提交:

$ 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
看到了吗?修补程序不适用,因为上下文不匹配。因此会出现冲突,您必须手动修复它


当您将bugger线一分为二时,补丁类似于:

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
现在,虽然匹配并不完美,但至少上下文中第一行未修改的内容是匹配的,因此合并可能会继续进行。

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

  • 在第1行插入
    1
  • 1之后的第2行和第3行插入
    这是\n bug
  • 删除第4行的
    错误
    ,在
    这是
    之后,替换为
    不是错误
  • 在第2行的
    1
    之后、
    之前插入
    2
    ,这是
  • 步骤2、3非常明确。尽管行号与步骤3中的预期不同,但上下文清楚地表明了这一点。在第二种情况下

  • 在第1行插入
    1
  • 1之后的第2行插入
    这是一个BUG

  • 删除第
    行这是一个BUG
    ,替换为第
    行后第3行的
    这不是BUG
  • 在第2行的
    1
    之后,在
    之前插入
    2
    ,这是一个BUG
  • 在这种情况下,修补程序#3是不可能的,因为
    这是一个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
    git-mergetool
    手动修复冲突,在重基失败时遵循提示

    您可以通过添加
    -s recursive-X theres
    -s recursive-X ours
    来指定策略,从而使重基“成功”。但是,由于上下文冲突,您的修复在这两种情况下都会失败

    git在使用--fixup和--autosquash时让我非常头疼

    注意,在使用git rebase--autosquash
    时,还有另一种情况令人头痛

    Git 2.20(2018年第4季度)刚刚修复了一个与autosquash相关的错误:“
    Git rebase-i”
    在运行“挤压/修复”时未正确清除状态文件,然后用户手动修改提交,该错误已得到纠正

    见(2018年8月31日)作者。 (于2018年9月24日合并)

    rebase-i--autosquash:演示跳过最后一次挤压的问题 git commit--squash命令不仅可以用于修改commit 消息和更改,还可以记录即将到来的重基的注释

    例如,当给定提交的作者信息不正确时,用户可能会调用
    git commit--allow empty-m“Fix author”-squash
    ,以提醒他们在重基期间修复该问题。 当编辑器弹出时,用户只需删除提交消息即可中止 在这一阶段,修复作者信息,然后继续执行
    git-rebase--skip
    。(这是一个真实世界的例子,它将Git for Windows的基础重新设置为v2.19.0-rc1。)

    然而,
    gitreb中有一个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
    
    $ 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
    
    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
    
    $ 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
    
    * 0192a To fixup
    * 02f12 fixup! To fixup
    * 03763 fixup! To fixup
    * 04ecb fixup! 02f12