如何压缩本地分支上的特定提交? [英] How do I squash specific commits on a local branch?

查看:116
本文介绍了如何压缩本地分支上的特定提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在那里找到几个接近我想要的答案,但是我担心我对Git没有足够的经验来完全理解如何实现我想要的。



给定一个提交了ABCDEFGHIJ的本地分支,我想将一些提交压缩在一起,这样我就可以结束,例如,相当于A-BCD-E-FGH-IJ。



这可能吗?目的是在重新绑定之后,但在合并到主分支之前,为此创建一个更加简明和翔实的历史记录,然后将其写入本地分行。 解决方案

你可以用 rebase 来做到这一点。假设提交AJ位于构建在 master 之上的本地分支 branchname 上,您可以这样做:

  git checkout branchname 
git rebase -i master

您可以通过如下交互式窗口阻止:

  pick A Commit消息A 
pick B提交消息B
pick C提交消息C
pick D提交消息D
pick E提交消息E
...



交互式底池为您的场景提供指示,因此您应该将pick改为挤压C和D相同的G,H和J):

  pick A提交消息A 
pick B提交消息B
squash C提交消息C
squash D提交消息D
pick E提交消息E

这会将B,C和D压缩成单个提交,允许您更改提交消息。



如果您乐意使用提交信息B的圣人,你可以使用fixup而不是squash,并且不会提示你更新提交信息。


I can find a few answers out there that come close to what I want, but I'm afraid I'm not really experienced enough with Git to exactly understand how to achieve what I want.

Given a local branch with commits A-B-C-D-E-F-G-H-I-J on it, I want to squash some of the commits together so that I end up with, for example, the equivalent of A-BCD-E-FGH-IJ.

Is this possible? The intention is to do this to a local branch, after rebasing, but before merging back into the master branch, in order to create a more concise and informative history.

解决方案

You can do this with rebase. Assuming commits A-J are on a local branch branchname built on top of master, you can do this:

git checkout branchname
git rebase -i master

You'll be prevented with an interactive window like this:

pick A Commit message A
pick B Commit message B
pick C Commit message C
pick D Commit message D
pick E Commit message E
...

Interactive rebase provides instructions for your scenario, so you should change "pick" to "squash" for C and D (and the same for G, H and J):

pick A Commit message A
pick B Commit message B
squash C Commit message C
squash D Commit message D
pick E Commit message E

This will squash B, C and D into a single commit, allowing you to change the commit message.

If you're happy to use the commit message of B, you can use "fixup" instead of "squash" and you won't be prompted to update the commit message.

这篇关于如何压缩本地分支上的特定提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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