How To Squash Your Branches Commits Before You Create a Pull Request

In this tutorial, you will learn about the benefits of having a clean history in your GIT repository and how you can squash your commits in order to achieve that. 

Performing code reviews in your team is a great tool to ensure good code quality.  The first hurdle most teams face when they start introducing code reviews is agreeing what the code submission process looks like.  There are two main approaches, you either prevent someone checking in code before it has been reviewed (my preference), or, you can do a review afterward.  From my experience, the after review processes never worked, mainly it takes a manual effort to enforce.  Even with the best intentions after a period of time, people get slack with reviews until eventually the code review gets ignored. 

If - like me - you are in the pre-review camp then the most likely approach you will take are pull requests.  When you use pull requests, you will usually have a development or integration branch.  When a team member wants to add the feature they've been working on into the main branch, they get their changes, create a new branch, create a pull request for people to approve their changes so it can be merged into the integration branch.  

A common problem can arise when developers start making changes and extra commits within their branch.  If a pull-request is merged in this situation, all the commit history of that branch will be added to the main branch.  

If you work in a big team, this can create a lot of noise in your commit history.  This noise can waste a lot of time.  If you need to find when certain issues were introduced, your committed history can be hard to read.

When you create a pull request, if your branch has multiple commits, you should squash those commits before pushing your pull request for review.

Squashing your commit is really easy to do.  It only takes three GIT commands.  Please do it.  To squash your commit, ensure you have the latest copy of your main branch locally.  Open up a terminal and type the following:

git checkout master
git merge --squash yourBranch
git commit -M "Message"

This will squash all your commits into a single commit, with a clean name.  If you ensure everyone on your team follows this process, you will have a clean GIT history.  For the people with OCD this should allow you to sleep a little more soundly, for everyone else, managing change will be easier


submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top