Can Git merge --squash preserve commit comments?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 10:16:07

问题


Is there a way of automatically adding all the commit comments from the squashed mybranch commits when doing a

git merge --squash mybranch

so that the single commit contains a concatenation of all the commit comments from mybranch


回答1:


I think that's what "git merge --squash" does automatically! Just do the commit with "git commit --no-edit" and I think that's what you'll get. I see all the commit messages inside .git/SQUASH_MSG immediately after I run the "git merge --squash" command. Do you?

Personally, I squash into master, but then force-push the result back to my topic branch. This way my final commit can be code-reviewed via pull-request.

Live example (try these exact commands, this uses my demo server, and really works):

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

At this point I recommend doing a "git push --force-with-lease" to get this squashed commit up to your remote topic branch ready for code-review.

Note: I do one fancy thing in the example. I'm actually squashing into origin/master, not local master, and I first do "git checkout branch" and "git reset --hard origin/master". This keeps my local master unperturbed by this operation, so that whatever I might be working on there is left alone.

I'm on Git 2.7.4. Maybe older gits behave differently, I don't know.

If you want to do this all in Web UI, and you happen to be using Atlassian Bitbucket Server (the on-premises version), you can install my Bit-Booster paid add-on, and hit the big "Squash" button it puts on pull-requests. It does the exact same thing, except that only non-merge commit messages are concatenated.



来源:https://stackoverflow.com/questions/41339160/can-git-merge-squash-preserve-commit-comments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!