Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Git merge --squash preserve commit comments?

Tags:

git

git-merge

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

like image 720
JonN Avatar asked Dec 27 '16 04:12

JonN


People also ask

What happens if you squash a merge commit?

The effect is very similar to what we've discussed before: all changes will be combined just as with a normal merge - but by using the --squash option, instead of a merge commit being automatically created, you're left with local changes in your working copy which you can then commit yourself.

Should you squash commits on merge?

Squash merges, as it's proponents argue, are more valuable than merge commits because entire new features or bug fixes can be compressed into a single commit and therefore easier to code review and read at some point in the future.

What is difference between merge commit and squash?

Squash merging is a merge option that allows you to condense the Git history of topic branches when you complete a pull request. Instead of each commit on the topic branch being added to the history of the default branch, a squash merge adds all the file changes to a single new commit on the default branch.

Is it good practice to squash commits?

Before you start, keep in mind that you should squash your commits BEFORE you ever push your changes to a remote repository. If you rewrite your history once others have made changes to it, you're asking for trouble… or conflicts. Potentially lots of them!


1 Answers

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 <[email protected]>
Date:   Mon Dec 26 22:07:50 2016 -0800

    Squashed commit of the following:

    commit 3120cbba4e94e0a81eed2f9ff42e7012cca996bf
    Author: G. Sylvie Davies <[email protected]>
    Date:   Thu Dec 15 18:24:02 2016 -0800

        b2

    commit ccb522334464879b8f39824031c997b57303475d
    Merge: 6b85efb 026bf0c
    Author: G. Sylvie Davies <[email protected]>
    Date:   Thu Dec 15 18:13:35 2016 -0800

        m

    commit 6b85efbddbb74d49a096bfc54fd4df15e261b72f
    Author: G. Sylvie Davies <[email protected]>
    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.

enter image description here

like image 122
G. Sylvie Davies Avatar answered Oct 30 '22 21:10

G. Sylvie Davies