Follow @endyourif rss Facebook LinkedIn

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

Published on Mar 3, 2019

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.

Tags: Github | git | squash

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP