Git Squash

Git Squash

Part of Git By Example Series

What is a Git SQUASH

Take several commits, joined them into one commit

Squash last n commits

git rebase -i HEAD~n

Example: Make 3 commits and sqash the last 2

By performing git log it can be seen 3 commits

The following steps are made with vi, so:

  • to start editing, press i on the keyboard

  • to end editing, press :wq on the keyboard

The next step will show the commits selected to alter.

Keep the commit "2" unaltered and the change the commit "3" from pick to squash or just use s .

Now the commit messages will be displayed. Here we can create a new message if we want.

Comment with # the unwanted messages and add your own.

Now git will perform all the operations and by executing git log , it can be seen that we have changed the commit history.

if the original commits haven't been pushed yet, then you can perform git push.
If the original where already pushed then perform git push --force , this will overwrite the git history.