git - Git rebase 链 master->featureA->featureB。master 发生了一些变化,为什么要再次 rebase featureB?
问题描述
我有一个 master 分支并从那里启动了 featureA 分支。然后我从 featureA 开始了 featureB 分支。然后我为master添加了一个带有一个文件的提交然后我从master重新设置了featureA但是之后由于某种原因试图从featureA重新设置featureB时我遇到了很多冲突。为什么以及如何避免它?
解决方案
请记住,分支只是一个提交的“标签”,在添加新提交时会自动向前移动。它们总是指向为此分支创建的最新提交。每个提交都指向其父级(或在合并提交的情况下指向其父级)。
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-a
和then feature-b
,那么 Git 假定feature-b
包含 2 个提交(b
、c
和d
)。有几个选项可以避免或解决此问题:
- 仅变基
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
推荐阅读
- unit-testing - 如何编写测试以确保编译错误?
- kotlin - 为什么这个表达式被认为是不可变的,因此可以使用 `val` 来定义?
- r - 使用 dplyr 查找具有最短日期的列的名称
- gmail-api - service.users().messages().get() 错误:java.io.EOFException:SSL 对等体错误关闭
- database - 如何解析 Redis AOF 文件?
- mysql - 将几个替代表表示为一个表?
- python - 如何在具有多行字符串的文件中搜索多个字符串
- html - Timevis - 使用自定义 css 更改字体大小
- javascript - 如何在canvas html内的多个图像上调用函数
- matlab - 使用 Matlab perfcurve 在交叉验证中绘制 ROC 曲线