git - 将压缩提交从主分支合并到功能分支时避免 git 中的冲突
问题描述
考虑以下做法:
- 开发人员从
main
功能分支中分支并根据需要创建尽可能多的提交 - 一旦功能完成,所有提交都被压缩并合并到
main
分支中(例如,想想 GitHub 的“压缩和合并”按钮)
现在这是一个让我感兴趣的用例:
- 创建和处理
feature1
分支 feature2
从分支的最后一次提交开始创建一个分支feature1
(参见下C
图中的提交)- 挤压并合并
feature1
到main
(参见提交G
) - 将这个新创建的提交
G
合并到feature2
分支中 - 继续在
feature2
分支上工作
换句话说,第 4 步中的合并G
到feature2
分支中看起来像这样:
user@host:~/repo (main)$ git checkout feature2
user@host:~/repo (feature2)$ git merge main # merge G into feature2
通常,这种合并(参见 commit H
)会导致许多合并冲突。
如何彻底消除这些冲突?
我能想到的最简单的解决方案如下(见下图):
user@host:~/repo (main)$ git checkout feature1
user@host:~/repo (feature1)$ git merge main # merge G into feature1; essentially, an empty commit
user@host:~/repo (feature1)$ git checkout feature2
user@host:~/repo (feature2)$ git merge feature1 # merge G' into feature2
换句话说,我们不是直接merge G
into feature2
,而是先merge G
into feature1
,然后merge feature1
into feature2
。
有没有更简单的方法?
解决方案
我会feature2
在压缩和合并main
之后重新定位。feature1
就像是:
git checkout feature2
git rebase --onto main C feature2
这C
将feature2
在main
.
但是我不确定它是否更简单。一个缺点是您将main
在生成的分支中获得提交(但我猜这最终是您想要的)。
个人意见:如果分支被其他人用作参考,请不要首先使用 squash。
推荐阅读
- python - 为什么视图对象在 Python sorted() 函数的上下文中是可索引的?
- python - 在python中查询集合的字典
- c# - 如何从 xls (Excel97) 文件中读取 excel 应用程序版本?
- javascript - javascript 每个都使用最后一个变量值
- http - 使用 HTTP 模块保护请求
- javascript - 复选框的 CSS 标签需要为单个类指定
- amazon-cloudformation - 来自 Cloudformation / 无服务器的 AWS SES 模板
- reactjs - 如何在我的 React 组件中使用 SCSS 变量
- microsoft-graph-api - 如何使用 Microsoft API 将文件上传到不同域共享的文件夹?
- javascript - 使用 JavaScript 在 IE11 中禁用 Alt+Home