Squash latest commits in one in Git

Posted on December 9, 2019

So you have made some commits so small that you want to merge them on just one. If you have uncommited changes, commit them before you continue. To merge the last five commits on one run:

git rebase -i HEAD~5

This line will give this file to edit:

pick d71558e menu fix
pick cda22e1 firefox menu editor bugfix
pick 96a804f fixes
pick c70fcc7 fixes
pick a90bf44 new version

# Rebase 7594210..a90bf44 onto 7594210 (5 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Change the first lines like that:

pick d71558e menu fix
s cda22e1 firefox menu editor bugfix
s 96a804f fixes
s c70fcc7 fixes
s a90bf44 new version

Close and save the file. Now the four last commits will squash in the first one. If something went wront and got an error run

git rebase --abort

#and try again
git rebase -i HEAD~5

 

More info

https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History