重新设置以压缩同一分支上的多个提交 [英] rebase to squash multiple commits on same branch

查看:71
本文介绍了重新设置以压缩同一分支上的多个提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用rebase将多个提交压缩到同一分支上的单个提交中吗?

Can we use rebase to squash multiple commits into one single commit on the same branch?

以一个示例为例,我已经创建了两个主题分支-master的issueA_1和issueA_2.如图所示,我在两个分支中都进行了两次提交(为简洁起见,我在这里使用提交名称而不是提交哈希):

Taking an example, i have created two topic branches - issueA_1 and issueA_2 from master. I make a couple of commits in both branches, as can be seen in the diagram(i am using commit names here instead of commit hashes for brevity) :

           -->  (issueA_1) - commitX1
          /
(master)--
          \
           -->  (issueA_2)
                   |
                commitY1
                   |
                commitY2

然后我这样做:

git checkout issueA_2
git rebase -i issueA_1

我将变基文件更改为:

pick commitY1
fixup commitY2

此重新设置后,提交历史记录如下:

After this rebase, the commit history looks like this :

           -->  (issueA_1) - commitX1
          /
(master)--
          \
           -->  (issueA_2)
                   |
                commitX1
                   |
                commitY1
                   |
                commitY2

我不再需要分支issueA_1,所以我需要:

I don't need the branch issueA_1 anymore, so i do :

git branch -D issueA_1

issueA_2的提交历史尚不足以合并到master中.我希望在合并到master之前将该分支的commitX1,commitY1和commitY2压缩为1个单个提交.这可能吗?

The commit history of issueA_2 isn't pretty enough to be merged into master yet. I want commitX1, commitY1 and commitY2 of this branch to be squashed into 1 single commit before i merge into master. Is this possible?

推荐答案

是的,我发现的最简单的方法是:

Yes, the simplest way I find is to do:

git rebase -i HEAD~3

这将使您查看最近的3次提交,然后可以将它们压扁.

This will let you review the last 3 commits and you can then squash them.

这篇关于重新设置以压缩同一分支上的多个提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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