Git: Are you an over-committer? Squash those commits!

Stacie Taylor-Cima
Mar 1, 2018 · 3 min read

Are you an over-committer? Me too. Basically, anytime I complete some kind of task, I commit it with a message that details what I’ve done. I do this for a few reasons:

  • I’ve read over and over again: “Commit a lot, commit often”
  • I have kids and work at home. As I’m working, my kids could need me immediately at any second, and I want to make sure my work is saved.
  • Those crazy kids also sometimes get access to the computer and get happy fingers while my projects are open, so I want to make sure I’ve got it all backed up.
  • I often find myself working in short stints and having lots of commit messages helps me retrace my steps and offers me a sort of progress report.

I was recently told by someone who was reviewing my code that I commit too frequently. He suggested, “If you feel that it is helpful for you [to make incremental commits], I would recommend you check out squashing commits with Git.”

After researching what squashing commits even meant, I realized it was the perfect way for me to have my cake and eat it too! It allows you to make as many smaller commits locally as you feel necessary, based on your own preference and workflow, and then squash them down into one clean commit so that your remote repo’s commit history is nice and tidy!

Below, I’ve laid out a step-by-step guide on how to squash commits that can be followed by any Code Newbie!


Steps to squash your commits:

WARNING! Before you start, keep in mind that you should squash your commits BEFORE you ever push your changes to a remote repository. If you rewrite your history once others have made changes to it, you’re asking for trouble… or conflicts. Potentially lots of them!

  • Create and checkout a new branch where you will write your code:git checkout -b [your_branch_here]
  • As you work on your project, make as many commits as you’d like!
  • Find out how many commits you want to squash! You will need the number of commits so that you can tell Git how far to go back during your rebase. The command below will get you the log of commits not including any that are available to master and you can count how many you’d like to squash:git log [your_branch_here] — not master
  • Run an interactive rebase. In the sample below, replace X with the number of commits you want to squash. This will rebase you X commits back: git rebase -i HEAD~X
  • The text editor will pop up and list your commits. Shift + i to type. Replace pick with squash on the most recent commits. Make sure the first commit is still preceded with the pick command. This basically tells Git to squash all of your most recent commits into your first commit.
  • esc to escape insert mode.
  • :wq to save your changes. (VIM)
  • Another text editor screen will pop up and allow you to write a new commit message that will sum up all of your previous commit messages. Comment out all of the previous commit messages and write your new one.
  • :wq to save and quit. (VIM)
  • You can double check that it worked by running git log again. You should see only your new single commit!

There are multiple variations of this process and countless workflows and commit preferences among us. I am really interested in how you commit and how you squash (if you do). Please share your experiences so that we can all become more productive and efficient developers and team players!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade