Git南瓜在分支的中间提交 [英] Git squash commits in the middle of a branch

查看:79
本文介绍了Git南瓜在分支的中间提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在分支的中间将多个提交压缩在一起,而又不修改前后的提交.

I want to squash several commits together in the middle of a branch without modifying the commits before and after.

我有:

A -- B -- C -- D -- E -- F -- G
|                             |
master                        dev
origin/master

我想把它塞进去

A -- H -- E -- F -- G
|                   |
master              dev
origin/master

其中H等效于B -- C -- D.而且我希望能够指定H的提交消息. A是上次推送的提交,因此可以重写之后的所有提交而不会弄乱服务器.这个想法是在我快进master之前清理历史记录.

Where H is equivalent to B -- C -- D. And I want to be able to specify the commit message of H. A is the last commit that has been pushed so all commits after that can be rewritten without messing up the server. The idea is to clean up the history before I fast forward master.

我该怎么做?

PS:请注意,在我的情况下,我实际上在中间要压扁3次以上,但是如果我可以用3次压扁,那么我应该可以用更多的次数压扁.

PS: Note that in my case I actually have a lot more than 3 commits to squash in the middle, but if I can do it with 3, I should be able to do it with more.

PPS:此外,如果可能的话,我更希望采用一种解决方案,使EFG保持不变(主要是关于提交日期).

PPS: Also, if possible, I would prefer a solution where E, F and G remain untouched (mostly regarding the commit date).

推荐答案

您可以进行交互式变基,并手动选择要压榨的提交.这将重写您的dev分支的历史记录,但是由于您尚未推送这些提交,因此除了在您自己的计算机上可能发生的情况之外,不会有任何负面后果.

You can do an interactive rebase and hand select the commits which you want to squash. This will rewrite the history of your dev branch, but since you have not pushed these commits, there should not be any negative aftermath from this besides what might happen on your own computer.

从以下内容开始:

git checkout dev
git rebase -i HEAD~6

这应该会弹出一个窗口,向您显示以下7个提交列表,从dev分支的HEAD返回6个步骤:

This should bring up a window showing you the following list of 7 commits, going back 6 steps from the HEAD of your dev branch:

pick 07c5abd message for commit A
pick dl398cn message for commit B
pick 93nmcdu message for commit C
pick lst28e4 message for commit D
pick 398nmol message for commit E
pick 9kml38d message for commit F
pick 02jmdmp message for commit G

显示的第一个提交(上面的A)是最旧的,最后一个是最新的.您可以看到,默认情况下,每个提交的选项为pick.如果您现在完成了变基,那么您将只保留每个提交,这实际上是一个空操作.但是,由于您要压缩某些中间提交,因此请编辑列表并将其更改为:

The first commit shown (A above) is the oldest and the last is the most recent. You can see that by default, the option for each commit is pick. If you finished the rebase now, you would just be retaining each commit as it is, which is effectively a no-op. But since you want to squash certain middle commits, edit and change the list to this:

pick 07c5abd message for commit A
pick dl398cn new commit message for "H" goes here
squash 93nmcdu message for commit C
squash lst28e4 message for commit D
pick 398nmol message for commit E
pick 9kml38d message for commit F
pick 02jmdmp message for commit G

请仔细注意上面发生的事情.通过输入squash,您告诉Git将提交合并到它的上方中,该提交是紧接在之前的提交.因此,这表示将提交D压回至提交C,然后将C压入B,只剩下一次提交用于提交BCD的提交.其他提交保持不变.

Note carefully what is happening above. By typing squash you are telling Git to merge that commit into the one above it, which is the commit which came immediately before it. So this says to squash commit D backwards into commit C, and then to squash C into B, leaving you with just one commit for commits B, C, and D. The other commits remain as is.

保存文件(在Windows中的Git Bash上为:wq ),完成了重新设置基准.请记住,您可能会像预期的那样从中获得合并冲突,但是解决它们并没有什么特别的,您可以像使用任何常规的rebase或merge一样继续进行.

Save the file (: wq on Git Bash in Windows), and the rebase is complete. Keep in mind you can get merge conflicts from this as you might expect, but there is nothing special about resolving them and you can carry on as you would with any regular rebase or merge.

如果在重新设置基准后检查分支,您会注意到EFG提交现在具有新的哈希,日期等.这是因为这些提交实际上已被new替换.提交.这样做的原因是您重写了历史记录,因此,一般而言,提交不再可以与以前的提交相同.

If you inspect the branch after the rebase, you will notice that the E, F, and G commits now have new hashes, dates, etc. This is because these commits have actually been replaced by new commits. The reason for this is that you rewrote history, and therefore the commits in general can no longer be the same as they were previously.

这篇关于Git南瓜在分支的中间提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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