Git merge --squash 可以保留提交注释吗?

Can Git merge --squash preserve commit comments?

有没有办法在执行

时自动添加压缩的 mybranch 提交中的所有提交注释
git merge --squash mybranch

以便单个提交包含来自 mybranch

的所有提交评论的串联

我认为这就是 "git merge --squash" 自动执行的操作!只需使用 "git commit --no-edit" 进行提交,我认为这就是您会得到的。在 运行 "git merge --squash" 命令之后,我立即在 .git/SQUASH_MSG 中看到了所有提交消息。你呢?

就我个人而言,我挤进 master,然后将结果强制推回我的主题分支。这样我的最终提交可以通过拉取请求进行代码审查。

现场示例(尝试这些确切的命令,这使用我的演示服务器,并且确实有效):

git clone http://vm.bit-booster.com/bitbucket/scm/bb/rebase-example-2.git
cd rebase-example-2
git checkout branch
git reset --hard origin/master
git merge --squash origin/branch
git commit --no-edit

git show
commit 74656c51212526af49382c985419244737141217
Author: G. Sylvie Davies <sylvie@bit-booster.com>
Date:   Mon Dec 26 22:07:50 2016 -0800

    Squashed commit of the following:

    commit 3120cbba4e94e0a81eed2f9ff42e7012cca996bf
    Author: G. Sylvie Davies <sylvie@bit-booster.com>
    Date:   Thu Dec 15 18:24:02 2016 -0800

        b2

    commit ccb522334464879b8f39824031c997b57303475d
    Merge: 6b85efb 026bf0c
    Author: G. Sylvie Davies <sylvie@bit-booster.com>
    Date:   Thu Dec 15 18:13:35 2016 -0800

        m

    commit 6b85efbddbb74d49a096bfc54fd4df15e261b72f
    Author: G. Sylvie Davies <sylvie@bit-booster.com>
    Date:   Thu Dec 15 18:12:51 2016 -0800

        b1

在这一点上,我建议做一个 "git push --force-with-lease" 将这个压缩的提交提交到您的远程主题分支,为代码审查做好准备。

注意:我在示例中做了一件奇特的事情。我其实是挤成origin/master,不是土豪,先做"git checkout branch"和"git reset --hard origin/master"。这让我的本地 master 不受此操作的干扰,因此无论我可能在那里做什么,都不会受到影响。

我在 Git 2.7.4。也许老 git 的行为不同,我不知道。

如果你想在 Web UI 中完成这一切,并且你碰巧使用的是 Atlassian Bitbucket Server(本地版本),你可以安装我的 Bit-Booster 付费插件,然后点击它放在拉取请求上的大 "Squash" 按钮。它做完全相同的事情,除了只有非合并提交消息被连接起来。