Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't `git merge <branch> --squash` make a commit?

When doing a normal merge, e.g. git merge <branch>, git creates a merge commit and updates the HEAD of the current branch to that commit.

When doing a squash merge, e.g. git merge <branch> --squash, however, it does not make a commit, saying (on a clean merge):

Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested

What's the rationale behind this? To me, this seems like an unexpected difference between a normal merge and a squash merge. It would be more intuitive to me if the only difference between a normal merge and a squash merge were that a squash merge squashes commits.

like image 832
Luke Avatar asked May 22 '18 19:05

Luke


People also ask

Does squash and merge create a new commit?

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.

How do you squash commits when merging?

To enable commit squashing as the default option in your repository: Navigate to your chosen repository and open the Settings sub-tab. Open the General Settings page. Check the box for Squash commits on merge default enabled.

What happens if you squash a merge commit?

Although this may seem counterintuitive, it works when you need to set up changes in your local repo that can then be committed manually. More specifically, squashing during a merge generates the working tree and index state to match a merge without actually creating a merge commit.

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.


1 Answers

What's the rationale behind this?

The draft merge commit message will contain all your squashed messages from the other branch. Something like this:

Squashed commit of the following:

commit 2fb77b77f813501ae2c8159e7bf751c216572a57
Author: Your Name <[email protected]>
Date:   Tue May 22 22:47:50 2018 +0200

    Drop baz

commit 894f1ef07af29d25c4716dce9db4402032f854d4
Author: Your Name <[email protected]>
Date:   Tue May 22 22:47:39 2018 +0200

    Fix bar

commit 7f60998ab1949e9e8db9229f9ef9e7c0333cd04f
Author: Your Name <[email protected]>
Date:   Tue May 22 22:47:19 2018 +0200

    Add foo

Usually, you will want to customize that message before committing.


If you are happy with the default message you could do:

git merge <branch> --squash && git commit --no-edit
like image 87
sergej Avatar answered Sep 21 '22 05:09

sergej