git rebase 交互:将提交合并在一起 [英] git rebase interactive: squash merge commits together

查看:21
本文介绍了git rebase 交互:将提交合并在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个简单的解决方案来在交互式变基期间将两个合并提交压缩在一起.

I wanted to have a simple solution to squash two merge commits together during an interactive rebase.

我的存储库看起来像:

   X --- Y --------- M1 -------- M2 (my-feature)
  /                 /           /
 /                 /           /
a --- b --- c --- d --- e --- f (stable)

也就是说,我有一个 my-feature 分支,它最近被合并了两次,中间没有真正的提交.我不只是想重新设置 my-feature 分支,因为它是它自己的已发布分支,我只想将最后两个合并提交合并为一个(尚未发布这些提交)还)

That is, I have a my-feature branch that has been merged twice recently, with no real commits in between. I don't just want to rebase the my-feature branch since it is a published branch of its own, I just want to squash together the last two merge commits into one (haven't published those commits yet)

   X --- Y ---- M (my-feature)
  /            /
 /            /
a --- ... -- f (stable)

我试过了:

git rebase -p -i M1^

但我得到了:

Refusing to squash a merge: M2

我最后做的是:

git checkout my-feature
git reset --soft HEAD^  # remove the last commit (M2) but keep the changes in the index
git commit -m toto      # redo the commit M2, this time it is not a merge commit
git rebase -p -i M1^    # do the rebase and squash the last commit
git diff M2 HEAD        # test the commits are the same

现在,新的合并提交不再被视为合并提交(它只保留了第一个父项).所以:

Now, the new merge commit is not considered a merge commit anymore (it only kept the first parent). So:

git reset --soft HEAD^               # get ready to modify the commit
git stash                            # put away the index
git merge -s ours --no-commit stable # regenerate merge information (the second parent)
git stash apply                      # get the index back with the real merge in it
git commit -a                        # commit your merge
git diff M2 HEAD                     # test that you have the same commit again

但是如果我有很多提交,这会变得复杂,你有更好的解决方案吗?谢谢.

But this can get complicated if I have many commits, do you have a better solution ? Thanks.

米尔德里德

推荐答案

这是一个老话题,但我在寻找类似信息时偶然发现了它.

This is an old topic, but I just ran across it while looking for similar information.

类似于子树章鱼合并 是解决此类问题的好方法:

A trick similar to the one described in Subtree octopus merge is a really good solution to this type of problem:

git checkout my-feature
git reset --soft Y
git rev-parse f > .git/MERGE_HEAD
git commit

这将采用存在于 my-feature 尖端的索引,并使用它从 Y 创建一个新的提交,'f' 作为第二个父级.结果就像你从来没有做过M1,而是直接去执行M2一样.

That will take the index as it exists at the tip of my-feature, and use it to create a new commit off of Y, with 'f' as a second parent. The result is the same as if you'd never performed M1, but gone straight to performing M2.

这篇关于git rebase 交互:将提交合并在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆