有没有一种方法可以在git rebase中自动丢弃修改后的提交?

3
以下情况是我在使用 Git 时偶尔遇到的情况。一开始有一个线性历史,有两个分支。主分支当前指向 C。分支 A 是我要求同事合并到主分支的分支,而分支 B 则有未完成的工作,我还没有公开它。
 C
  \ 
   A - B

在代码审查期间,我的同事注意到分支A的最后一次提交中有一个错字。为了不必创建一个新的提交来修复这个问题,我修改了有问题的提交,然后将其合并到主分支上。现在主分支指向A',也就是纠正了错字的提交。

C - A'
 \
   A - B

下一步是将未完成的工作变基。我希望最终结果看起来像这样:
C - A'
     \
      B

然而默认情况下,Git 假设 A 和 A' 是完全不相关的提交,并尝试执行如下操作:
C - A'
     \
      A - B

这最终引出了我的问题:
有没有一种方法可以让git自行判断我们不需要在rebase中包含A,因为A'已经在那里了?或者这是总是需要人工干预以删除重复提交的情况之一?
2个回答

2
这可以通过使用git rebase --skip命令来实现。
C - A'
     \
      B

当在主分支上进行变基操作,将分支B变基到主分支上后,你会看到以下内容 -
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch' to see the failed patch
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".

你可以执行 git rebase --skip 命令,这将跳过该提交,使git历史记录更加干净。


2

虽然它不会做你所要求的事情,但在这种情况下,我已经习惯了始终使用 git rebase -i。它可以让你:

  • Remove A from the to-do list first.
  • Avoid creating an A' commit that isn't already an ancestor to B. You instead can commit the typo fix as D:
    C
     \
      A - B - D
    
    And then when running git rebase -i C, you can reorder the to-do list to make D a fixup commit to A. Then you'll end up with:
    C
     \
      A' - B
    
    which is your desired end state. (If you commit D via git commit --fixup A, then the reordering will happen automatically when running git rebase -i if git.autosquash = true.)

不幸的是,如果我有两个分支,一个指向A,另一个指向B,这并没有帮助。如果我对其中一个进行变基,那么另一个将继续指向历史版本的旧版本,其中将包括A而不是A'。(除非有一种同时变基两个分支的方法...有吗?) - hugomg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接