Git - Squashing multiple commits into one when merging a branch Git - Squashing multiple commits into one when merging a branch

When you are using Github, it's quite common to branch your work while working on a feature. Many times your branch contains a lot of small commits that when you merge it into your main branch you want a single commit message describing the full work in that branch.

Git offers a handy command called git merge --squash [your-branch]. This is then a followed by a regular git commit and git push.

Squashing a branch with a single commit

I commonly use this functionality when I create a feature branch and want to work with one or more team members. In this scenario we do a lot of micro commits to continually share our code as we progress. When the feature is fully completed, we do not want all of this micro commits cluttering up our commit history. Instead we would like a single commit message describing the entire feature.

In this example we will assume that we have a branch called FeatureX:

git checkout development
git pull
git merge --squash FeatureX
git commit -m "Completed FeatureX work"
git push

If you are familiar with merging regular branches, you will notice that it is identical with the addition of the option --squash.

Published on Mar 3, 2019

Tags: Github | git

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.


Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.