在维护合并历史记录的同时变基

rebase whilst maintaing merge history

我最喜欢的 git 工作流程是 rebase 提交到 master 之前的所有内容,而且还利用 git merge --no-ff 创建历史记录可读、可识别的工作块,以及这些工作块中的精细细节。

使用 --no-ff 给出的历史看起来有点像这样:

|
|\
| \
|  |FeatureA.InterestingStep1
|  |FeatureA.InterestingStep2
|  |FeatureA.InterestingStep3
|  |FeatureA.InterestingStep4
|  |FeatureA.InterestingStep5
| /
|/
|FeatureA
|\
| \
|  |FeatureB.InterestingStep1
|  |FeatureB.InterestingStep2
|  |FeatureB.InterestingStep3
| /
|/
|FeatureB

没关系。

遗憾的是,如果我尝试对其进行变基,那么我就会失去结构。

假设我已经完成我的工作、获取、重新设置基准、清理合并冲突、清理我本地混乱的提交历史成为我想要在 master 上的(仍然是多个)最终提交并构建必要的 --no-ff结构。

然后我推动,但团队中的其他人先发制人,origin\master 移动了。 如果我变基,那么 -no-ff 结构就会消失,我只剩下一个线性提交序列。

重新创建它并不是世界末日,但是有没有办法在不丢失结构的情况下变基?

你想要的是rebase-p(或--preserve-merges)选项,它保留合并提交

来自doc

Recreate merge commits instead of flattening the history by replaying commits a merge commit introduces. Merge conflict resolutions or manual amendments to merge commits are not preserved.

加入@Francesco 您可能还想留意许多开发人员使用的 git pull --rebase。也抹平了历史。避免这种情况的方法是手动执行 fetch/rebase 即(在 master 上)

git fetch

git rebase --preserve-merges origin/master