git merge --squash

I’m a fan of rebasing in git. I like being able to make lots of small, messy commits without worrying about a readable history and cleaning it up later. But on occasion I’ve had issues where rebasing and squashing my commits has resulted in many merge conflicts. I’m not really sure why this sometimes happens but it is a pain. Fixing merge conflicts can be error prone and time consuming.

Thankfully I’ve recently come across git merge --squash which can help in these situations. To use it do the following:

# Checkout master or whatever you branched off
git checkout master

# Create a new, clean branch
git checkout -b fancy-new-clean-branch

# Now merge the old, messy branch into the new
# branch with git merge --squash
git merge --squash old-messy-branch

# This will copy all the changes to the current
# branch but will NOT create a new commit. Add all
# the changes and then commit. 
git add .
git commit -m 'New easy to understand commit'

Now your git history on the new branch will have a new single commit that you can push up and create a pull request for.