Git: How to Squash Commits

March 19 2015, 7:18pm

Let's say you have several commits you'd like to combine into a single commit. Or an open source project you made a pull request to asked you to squash all your commits into one. You can squash them and they don't even have to have happened one after another.

git rebase -i HEAD~n

Where n is the number of the commits into the commit history to show to pick from.

It will open vim and look something like this

Now just change the word "pick" in front of the commit you want to squash to "squash" or "s". You can re-order commits if you want. Ones you change to "s" will be merged into the commit above it.

Then save and quit like you normally do, and it'll take you to a second page in vim where you can construct the merged commit message from the previous 2 commit messages. Save and exit.

Now just push up the changes: git push --force origin [branch-name]

Done!

For a more detailed explanation see the git manual: http://git-scm.com/docs/git-rebase#_interactive_mode