Skip to content

Instantly share code, notes, and snippets.

@johngorithm
Last active October 16, 2019 08:40
Show Gist options
  • Save johngorithm/568044cd50ed0211526e7257fd7822e1 to your computer and use it in GitHub Desktop.
Save johngorithm/568044cd50ed0211526e7257fd7822e1 to your computer and use it in GitHub Desktop.
The Best Approach to Squashing Commits

This can be done easily without git rebase or git merge --squash. In this example, let's squash the last 2 commits.

If you want to squash and write a new commit message, use the command below

git reset --soft HEAD~2

The 2 above points at the last 2 commit counting from the HEAD which is the top most commit in history
followed by

git commit


To squash and edit the combination of the existing commit messages, use the commands below

git reset --soft HEAD~2 && 
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"
The second command opens the combination of the commit messages in commits involved for editing. The last is the
commit message of the HEAD.

Reverting the above Git Commit Squashing Operations

git reset 'HEAD@{1}'

Thank You.

Copyright @johngorithm

Software Developer @Ginger.io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment