Read more

Squashing several Git commits into a single commit

August 25, 2010Software engineer at makandra GmbH

This note shows how to merge an ugly feature branch with multiple dirty WIP commits back into the master as one pretty commit.

Squashing commits with git rebase

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show snapshot

What we are describing here will destroy commit history and can go wrong. For this reason, do the squashing on a separate branch:

git checkout -b squashed_feature

This way, if you screw up, you can go back to your original branch, make another branch for squashing and try again.


If you didn't make a backup branch and something goes wrong you can still find your old commits in the reflog Show snapshot .

To squash all commits since you branched away from master, do

git rebase -i master

Note that rebasing to the master does not work if you merged the master into your feature branch while you were working on the new feature. If you did this you will need to find the original branch point and call git rebase with a SHA1 revision.

Your editor will open with a file like

pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3

Each line represents a commit (in chronological order, the latest commit will be at the bottom).

To transform all these commits into a single one, change the file to this:

pick fda59df commit 1
squash x536897 commit 2
squash c01a668 commit 3

This means, you take the first commit, and squash the following onto it. If you remove a line, the corresponding commit is actually really lost. Don't bother changing the commit messages because they are ignored. After saving the squash settings, your editor will open once more to ask for a commit message for the squashed commit.

You can now merge your feature as a single commit into the master:

git checkout master
git merge squashed_feature

When there are many merge conflicts throughout commits

Your console might have as a rebase status something like rebase-i 1/15

When you try to rebase and you have to resolve a lot of commits one by one, it might be an easier workflow when you squash all commits from your current branch first.

For this you can use
git rebase -i HEAD~20

The number can be higher than the actually required commits to be squashed since you can choose which commit to squash within interactive mode.

  • For finding which commits you have to squash using tig or git log --oneline will be useful.
    • git log --oneline ..master and git log --oneline master.. might be useful as well

Alternative: Reset your feature branch to the master state

Another way to turn multiple commits in a feature branch into a single commit is to reset the feature branch changes in the master and commit everything again.

# Switch to the master branch and make sure you are up to date.
git checkout master
git fetch # this may be necessary (depending on your git config) to receive updates on origin/master
git pull

# Merge the feature branch into the master branch.
git merge feature_branch

# Reset the master branch to origin's state.
git reset origin/master

# Git now considers all changes as unstaged changes.
# We can add these changes as one commit.
# Adding . will also add untracked files.
git add --all
git commit

Note that this is not touching the feature branch at all. If you would merge the feature branch into the master again at a later stage all of its commits would reappear in the log.

You may also do it the other way round (merging master into the branch and resetting to the master state) but this will destroy your commits in the feature branch, meaning you can not push it to origin.

August 25, 2010Software engineer at makandra GmbH
Posted by Lexy to makandra dev (2010-08-25 14:21)