如何在git交互式rebase中将提交与下一个提交合并? [英] How to merge a commit with the next commit in git interactive rebase?

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

问题描述

git rebase -i 允许通过 squashfixup 将提交与前一个合并.这两个选项都需要至少一个提交被 pick-ed.当一个人想要使用第一个提交但丢弃它的提交消息时呢?换句话说,如果我想让第一个提交与后续提交合并,我该怎么办?

git rebase -i allows one to merge a commit with the previous one through squash or fixup. Both the options require at least one commit to be pick-ed. What about the case when one wants to use the very first commit but discard its commit message? In other words, if I want the very first commit to be merge with the subsequent ones, what should I do?

推荐答案

你只需要 --root 标志(自 Git 1.7.12 起可用,即,除了某些未命名的从未更新咳*Centos*咳 Linux 发行版).

You just need the --root flag (available since Git 1.7.12, i.e., to everyone except certain unnamed never-updated cough*Centos*cough Linux distributions).

考虑挤压 AB 的结果是(有点——提交不完全遵守这个简单的代数,但它适用于我们的案例)只是A + B.因此,由于 B + A = A + B,无论是将提交 #1 压缩到提交 #2,还是将 #2 压缩到提交 #1,都没有关系.当然,这不包括压缩在一起的提交的消息,所以让我们仔细看看.

Consider that the result of squashing A and B is (sort of—commits don't quite obey this simple algebra, but it works for our case) just A + B. Therefore, since B + A = A + B, it does not matter whether you squash commit #1 into commit #2, or commit #2 into commit #1. Of course this does not count the squashed-together commit's message, so let's take a closer look at that.

假设您已经运行了 git rebase -i --root 以便能够对包括根提交在内的所有内容进行变基.您现在拥有:

Suppose you have run git rebase -i --root so as to be able to rebase everything including the root commit. You now have:

pick cb57fdd initial commit
pick 478acbd second commit

将第二个更改为squash,然后写入文件并退出编辑器.Git 将两次提交压缩在一起并再次打开您的编辑器,您会看到:

Change the second one to squash and write the file and exit the editor. Git squashes the two commits together and brings up your editor again, and you see:

# This is a combination of 2 commits.
# This is the 1st commit message:

initial commit

# This is the commit message #2:

second commit

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Nov 19 14:44:07 2016 -0800
#
# interactive rebase in progress; onto 45bc10c
# Last commands done (2 commands done):
#    pick cb57fdd initial commit
#    squash 478acbd second commit

(具体细节取决于您的 Git 版本,当然还有提交内容).

(the precise details depend on your Git version, as well of course as the commit contents).

因为您使用的是您最喜欢的编辑器,所以很容易构建您想要的任何新提交消息.例如,您可以删除整个第一次提交的消息,如果这是您想要的.执行此操作,写入文件,然后退出编辑器,一切就绪.

Since you are in your favorite editor, it is easy to construct whatever new commit message you want. For instance, you can just delete the entire first commit's message, if that's what you want. Do that, write the file, and exit the editor, and you are all set.

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

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