Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use git merge --squash

I'm fairly new to git and only work by myself so I don't use many of the features it can do but I am running into a process that either I am thinking about it wrong or doing something wrong.

I have a master branch with 1 commit (init).

I have a develop branch with 180 commits.

Today I am finally ready to merge the develop branch into the master, I did some reading and found out about squash. This seems like something useful since I wouldn't pollute the master branch with the same WIP commits that are in the develop branch.

So I ran

git checkout master
git merge --squash develop
git commit

From here everything looks as I expected, master has 2 commits, develop still has 180. In my head I now check out develop again and continue working. I pushed to bitbucket and took a look around at my project to see this merge and noticed the following:

1 commit(s) on master and not on develop
179 commit(s) on develop and not on master

Is this just expected behaviour and I am supposed to ignore it or did I do something wrong.

like image 472
mg87 Avatar asked Apr 05 '16 22:04

mg87


People also ask

What happens when you squash and merge?

When you select the Squash and merge option on a pull request on GitHub.com, the pull request's commits are squashed into a single commit. Instead of seeing all of a contributor's individual commits from a topic branch, the commits are combined into one commit and merged into the default branch.

How do you do squash and merge?

You can choose to squash merge when completing a pull request in Azure Repos. Choose Squash commit under Merge type in the Complete pull request dialog to squash merge the topic branch.

Is it better to squash and merge?

You should consider using squash if your team prefers a linear project history. This means that the history held by your main branch should not contain merges. A squash merge makes it possible to keep changes condensed to a single commit, supporting this strategy nicely.


1 Answers

This is expected since git merges all your commits into a single commit, which will be a different one compared to the ones in your develop branch. Think of the commits as a container of a set of changes, if you change the contents you'll have a different one.

You'll either have to accept this scenario, or you could adapt your workflow by working in feature-branches, e.g. master - develop - feature-branch.

Once a feature is done you make a squash-merge from feature-branch to develop and delete the feature-branch. Now you can make merges from develop to master without all the WIP commits, e.g. when you make new releases or such.

like image 170
Emil Kantis Avatar answered Sep 20 '22 06:09

Emil Kantis