How do you squash commits in Git?

Squashing means joining several small commits into a single clean commit. There are three common ways.

1. Interactive rebase (change “pick” to “squash” for the commits to join):

git rebase -i HEAD~3

2. Reset soft: go back and make one new commit.

Terminal output

$ git reset –soft HEAD~3
$ git commit -q -m "Add feature"
$ git log –oneline
e56c328 (HEAD -> main) Add feature
310ae0b Base

3. Squash merge: git merge --squash feature stages all the changes of a branch, and then you commit once.

Do not squash commits which are already pushed and shared.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.