Git Squash Tutorial: How to Combine Git Commits for a Clear and Organized Commit History

command line git how-to

A while back I wrote about managing local git branches with git rebase and I mentioned that I always submit my code changes with one commit only. No matter how many commits I make during my development process in my local branch when I prepare my code for code review and push it to remote, I squash all commits into one single commit, with one single commit message using git rebase.

what is git squash

Git squash is just a nickname for running git rebase in interactive mode so you can combine multiple commits and their messages into one single commit.

when to use git squash

When you have multiple commits in your local branch and you want to make sure your commit history is clear and organized. One note though: squashing only works if the last n commits are part of the same bug fix of feature.

why squash commits

As I mentioned previously, this is a bit of a personal preference and I like to have only one commit message that describes the fix or feature I’m adding to the merge/pull request.

when not to use git squash

Keep in mind that by squashing commits you are combining commits. So you should never squash commits on a shared branch because that will end up rewriting history and other people pulling that branch might get merge conflicts.

how to use git squash

Commits can be combined using the git rebase command in interactive mode (with the “-i” flag). Here’s an example for you to follow:

Step 1: Check the git history

git log --oneline

git log --oneline will show you a list of all the commits in the repository with their abbreviated hash and commit message. Example:

b10e23d Fix typo
e0fa33c Fix bug in feature B
9dfefc4 Add feature B
a302b3e Add feature A

Step 2: Identify the commits that you want to squash

Count the number of commits you want to squash. In the example above we want to squash the top 3 commits, all part of feature B (9dfefc4, e0fa33c and b10e23d).

Step 3: Start interactive rebase

git rebase -i HEAD~N

where N is the number of commits. In our example, 3.

This command will open an interactive editor that allows you to modify the commit history. The oldest commit is at the top and the newest is at the bottom.

Step 4: Update each commit status

In the interactive editor, change ‘pick’ to ‘squash’ on every line except the first line (the oldest commit). Squashing is done from newest to oldest: newer commits are squashed into older commits.

pick   b10e23d Fix typo
squash e0fa33c Fix bug in feature B
squash 9dfefc4 Add feature B

Step 5: Update the final commit message

Save and close the editor. Git will combine the changes from the squashed commits into the oldest commit and open another editor to allow you to modify the commit message.

The editor will display all the commit messages from the squashed commits. Combine them as desired and save and close the editor.

Step 6: Update the remote branch

If you had previously pushed your work to a remote repo, you will have to push this last change with the --force flag:

git push -f origin BRANCH_NAME 

This command will overwrite the existing commit history in the remote branch with the new, squashed commits. Be careful when using the --force flag: only use it in your own branch, never in a shared branch.

And that’s all!

the joke is on me

You might have picked up that English is not my first language and quite often I get tripped up with words. Well, early on in my career as a professional software developer I would always mix the words up and actually say or write something along the lines of “squishing” my commits. LOL

To this day, I still have to think for a second before I say it. Squ–, squish ?, squaaaaash, squash commits….

Squish GIF - Find & Share on GIPHY


All right, go squis…, I mean, squash all those commits!


If you found this helpful, please share this article!

The post Git Squash Tutorial: How to Combine Git Commits for a Clear and Organized Commit History was originally published at flaviabastos.ca

Are you a musician? Check out my Etsy shop at codenchords.etsy.com

Related Posts

One thought on “Git Squash Tutorial: How to Combine Git Commits for a Clear and Organized Commit History

Comments are closed.