简体   繁体   English

Git - 压缩早期提交并在不同的分支上反映它

[英]Git - squashing earlier commits and reflecting it on different branches

I'm fairly new to Git and I'm trying to squash a series of earlier commits, but I'm having problems having this 'squashing' reflect on all branches. 我对Git很新,而且我正在尝试压制一系列早期的提交,但是我在所有分支上都有这种“挤压”的反应时遇到了问题。

I started with the following tree: 我从以下树开始:

A---->B---->C---->D---->E---->F---->G---->H (Branch 1)
                                          \
                                           \
                                            \
                                             I---->J---->K (Branch 2)

I want to 'squash' commits C, D, E and F, but I want this 'squashing' to be reflected on both Branches 1 and 2. 我想'压缩'提交C,D,E和F,但我希望这个'压缩'能够反映在分支1和分支2上。

I tried 'git rebase -i HEAD~7' and did the necessary changes (while on Branch 1), but this is what I ended with: 我尝试了'git rebase -i HEAD~7'并进行了必要的更改(在分支1上),但这是我结束的:

A---->B---->C'---->G---->H (Branch 1)
      \
       \
        \
         C---->D---->E---->F---->G---->H---->I---->J---->K (Branch 2)

When what I wanted to get was: 当我想得到的是:

A---->B---->C'---->G---->H (Branch 1)
                          \
                           \
                            \
                             I---->J---->K (Branch 2)

What am I doing wrong? 我究竟做错了什么? Is there a way to achieve the result I want? 有没有办法达到我想要的结果?

Thanks in advance. 提前致谢。

Ok, this is better than the cherry-pick answer in my comment because if you have a lot of commits to cherry-pick that can be rather tedious (it also avoids the creation of a third branch). 好吧,这比我的评论中的cherry-pick答案更好,因为如果你有很多提交cherry-pick ,这可能相当繁琐(它也避免了第三个分支的创建)。

The quickest thing to do is two rebase s. 最快的事情是两个rebase s。 The first is the one you specified. 第一个是你指定的那个。 Then you checkout branch 2 and rebase that on top of your already- rebase d branch 1, removing the commits you don't want ( C , D , E , F , G , and H ). 然后你checkout分支2并在你的rebase d分支1的顶部重新定义,删除你不想要的提交( CDEFGH )。 So basically, you'd have: 所以基本上,你有:

git checkout <branch 1>
git rebase -i HEAD~7

which you already did, followed by: 您已经做过,其次是:

git checkout <branch 2>
git rebase -i <branch 1>

When your default editor opens, simply remove the lines associated with commits C through H . 打开默认编辑器时,只需删除与提交CH关联的行。 The result will be what you were looking for. 结果将是您正在寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM