首页 > 解决方案 > Git rebase 链 master->featureA->featureB。master 发生了一些变化,为什么要再次 rebase featureB?

问题描述

我有一个 master 分支并从那里启动了 featureA 分支。然后我从 featureA 开始了 featureB 分支。然后我为master添加了一个带有一个文件的提交然后我从master重新设置了featureA但是之后由于某种原因试图从featureA重新设置featureB时我遇到了很多冲突。为什么以及如何避免它?

标签: git

解决方案


请记住,分支只是一个提交的“标签”,在添加新提交时会自动向前移动。它们总是指向为此分支创建的最新提交。每个提交都指向其父级(或在合并提交的情况下指向其父级)。

  e--    master
 /
a-b-c-d
    ^ ^
    | `- feature-b
    `--- feature-a

git rebase target是 的简写git rebase target current-branch,反过来又是 的简写git rebase --onto target target current-branch,即“获取所有可从 到达current-branch但不能从 到达的提交,target并将其更改重新应用为新提交target”。

运行git rebase master feature-a将变基提交b-c,但git rebase master feature-b会变基b-c-d

如果你 rebasefeature-athen feature-b,那么 Git 假定feature-b包含 2 个提交(bcd)。有几个选项可以避免或解决此问题:

  • 仅变基feature-b,然后feature-a在新的变基提交上重新创建
  • 使用 rebase 的显式调用:(git rebase --onto new_target old_base branchname例如git rebase --onto master old-feature-a feature-b
  • 使用交互式变基模式并删除所有已应用的提交:git rebase -i target feature-b
  • 运行定期 rebase 并在发生冲突时检查更改是否已应用。如果是这样,请跳过提交:git rebase --skip

注意。在做这样的事情之前创建备份分支总是好的,例如git branch backup-feature-b feature-b


推荐阅读