In this tutorial, you will learn about the benefits of having a clean commit history in your GIT repository and how you can squash your commits in order to achieve that. One of the main offenders from having a messy commit log history comes from the humble code review. 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 on what the code submission process looks like. There are two main approaches, you either prevent someone from checking in code before it has been reviewed (my preference) or, you can do a review afterwards. From my experience, reviewing code after it has been committed has never worked out well. We all have a million things to do every day. As deadlines and project time pressures occur, even with the best intentions, people get slack with reviews until eventually the code starts to rot and the review gets ignored.
If - like me - you are in the pre-commit code review camp then you will likely adopt a pull request culture. When you use pull requests, you will usually have a development or integration branch. When a team member wants to add the feature that they been working on into the main branch, they create a new feature branch off of the development branch, add their changes within it, create a new pull request for people to approve their changes and then push those changes to
origin. Someone else will then review the code and either reject the changes or accept the code.
A common problem can arise when developers start making changes within the feature. Developers might add commit messages like 'commit', 'update 2', or 'test' within their feature branch as they work and make code review amends. If the pull request is merged without cleaning it first, all the commit history of the feature branch will also be merged into the main branch. If you work in a big team, this can create a lot of noise in your commit history. When your commit history is hard to read, it can make it much harder to find bugs and changes in the codebase. This noise can waste a lot of time.
To avoid this mess, when you create a pull request, you should squash the feature branches commit history before pushing it. 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:
This command sequence 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 as your commit history will be nice and clean. For everyone else, managing change will be easier Happy Coding 🤘