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
Git Restore
Git Commit Amend
Git Reset
Git Revert
Git Stash
Git Reflog
Exercises & Assignments
Discard the changes of a file
Undo the last commit but keep the changes
Revert a commit with git revert
Fix the message of the last commit
Stash your changes and bring them back
Recover a deleted branch with git reflog
Interview Questions & Answers
What is git revert?
How to undo last commit using git reset?
What is the difference between git reset and git revert?
What is the difference between soft, mixed and hard reset in Git?
What is git stash and when do you use it?
How do you change the last commit message in Git?
How do you squash commits in Git?