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

Tags: github, git, terminal, webdev
Published: 2021-04-17 22:28:09

![Photo by Christin Hume on Unsplash](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07nk569g9oym90pax65x.jpeg)

_Photo by Christin Hume on Unsplash_

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.

```
pick first_commit
squash second_commit
squash third_commit
squash fourth_commit

# Your text editor will give you a bunch of other instructions and options here. Check them out. There is some really helpful information.
```

* `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.

```
# This is a combination of X commits.
# The first commit’s message is:
# First commit message

# This is the 2nd commit message:
# Second commit message

# This is the 3rd commit message:
# Third commit message

# This is the 4th commit message:
# Fourth commit message

New commit message here

# Git will list some other instructions and the changes you’re committing here.
```

* `: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!

<hr> 

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!