Squash Your GitHub Pull Requests

One of the tenets of the Chromatic development process is small actionable tickets, with a corresponding branch/pull request for each. This often results in one commit per pull request when the PR is created. Of course, we are also very active in code review so it is common for that pull request that started as one commit to accrue additional commits with fixes and improvements.

In the past year or so, GitHub has added two new options for merging pull requests in addition to the default of creating a merge commit: squashing your commits and rebase and merge. While rebasing vs. merging is an interesting debate on its own (read: a religious level debate whose waters I am not willing to wade into), it was the squash option that caught my eye.

When it comes time to merge a pull request, GitHub’s default workflow produces a merge commit connecting two work trees: the feature branch and the base branch, main in our case. However, it often makes more sense to produce a single commit representing the sum of the changes. GitHub’s ‘squash & merge’ option allows us to do this. With squash and merge, the master branch is updated with a single commit that includes all of the changes originally committed in a feature branch. What’s more, the interface automatically pre-populates the commit message with a list of commit messages present in that feature branch. This helps preserve the full scope of the original and follow-up changes in the commit history.

A screenshot of the GitHub merge call to action

This approach should feel quite relatable to those in some open-source communities like Drupal, where a change is discussed/reviewed with patches and a ‘committer’ then commits all of the changes once the issue is declared resolved, resulting in a single coherent commit representing the intended change.

In most cases in Chromatic’s workflow, squashing our commits makes sense since we try to keep our PRs focused. That being said, we don’t need to wed ourselves with any single approach. There are always exceptions, and when those come about, it is as simple as selecting a different option in GitHub’s pull request UI. Squash & merge is another tool in our toolbox. As always: pick the right tool for the job.

A screenshot of the GitHub merge call to action