How to Squash Commits with Git

Part of my Git workflow at Automattic includes getting a pull request going as soon as possible.

I find this workflow useful as I am learning the code base for the latest version of WordPress.com, which is completely different from any codebase I have touched before. Because of this, I try to commit often so that I can get feedback and collaborate with my coworkers.

And while committing often is great to get feedback in a pull request, I tend to like to squash all of the commits into one before I merge my pull request into master.

How I Squash Commits with Git

The first few times I squashed commits with Git were very nerve wracking as I was worried about nuking my changeset. But, as I have worked more with Git in a team setting, I have become very comfortable with these following steps:

  1. Get merge base git merge-base my-branch-name master
  2. Rebase git rebase --interactive {hash from merge base}
  3. Change pick to squash for all but first commit

Although I have grown comfortable with the above commands, I find that I sometimes still refer to this awesome article from edX about rebasing pull requests.

If you find you need a bit more explanation for how to squash commits with Git, I’d recommend giving that article a read.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.