Combine several small commits into one clean commit using interactive rebase or git reset --soft, and learn when you need to force push afterwards.
The short answer
#The quickest way to squash the last few commits is to move the branch back with git reset --soft, which keeps every change staged, and then make one new commit. Use interactive rebase instead when you want to pick which commits to combine.
_10git reset --soft HEAD~3_10git commit -m "Add retry logic to the API client"
If those commits were already pushed, you will need git push --force-with-lease to update the remote branch.
Why it happens
#While working, it is normal to make lots of small commits: "wip", "fix typo", "actually fix it". They are useful as save points but noisy in the shared history. Squashing rewrites those commits into one, so the history explains what changed and why.
Squashing always creates a new commit with a new hash. The old commits are no longer on the branch. That is fine on your own feature branch, but it matters once other people have based work on the old commits.
How to fix it
#Reset soft and recommit
git reset --soft moves the branch pointer back but leaves your files and the staging area untouched. Everything from the removed commits is staged, ready to commit again as one.
_10git log --oneline -5 # check how many commits to squash_10git reset --soft HEAD~3 # undo the last 3 commits, keep the changes staged_10git commit # write one new message
This is simple and hard to get wrong, but it squashes everything after the reset point and you write the message from scratch.
Squash everything since you branched
If you want the whole feature branch as one commit, reset to the point where it left main:
_10git reset --soft $(git merge-base HEAD main)_10git commit -m "Add user profile page"
Interactive rebase
Interactive rebase lets you choose commit by commit. Start it with the number of commits to look at:
_10git rebase -i HEAD~4
Git opens an editor with one line per commit, oldest first:
_10pick a1b2c3d Add retry helper_10pick d4e5f6a Fix typo_10pick 0a1b2c3 Handle timeouts_10pick 9f8e7d6 Update tests
Leave the first line as pick and change the others to squash (or s) to merge them into the commit above and edit the combined message, or to fixup (or f) to merge them and throw their messages away. Save and close the editor, and Git does the rest.
_10pick a1b2c3d Add retry helper_10fixup d4e5f6a Fix typo_10squash 0a1b2c3 Handle timeouts_10fixup 9f8e7d6 Update tests
Plan the squash with fixup commits
If you already know a new commit belongs with an earlier one, mark it as you make it:
_10git commit --fixup a1b2c3d_10git rebase -i --autosquash main
--autosquash moves each fixup commit under its target and marks it for you, so you only need to save the todo list.
Squash when merging the pull request
GitHub, GitLab and Bitbucket all offer a "squash and merge" option. If your team uses it, you do not need to squash locally at all; the branch lands on main as one commit.
Push the result
If the branch was already on the remote, a normal push is rejected because the histories no longer match. Force push, but use the lease option so you do not overwrite commits someone else pushed in the meantime:
_10git push --force-with-lease
Common pitfalls
#- Squashing commits that are already on main or on a branch other people use. Everyone else then has commits that no longer exist upstream. Only rewrite your own branches.
- Using
git reset --hardby mistake. That throws the changes away rather than keeping them staged. If it happens,git reflogcan usually get them back. - Counting wrong with
HEAD~n. Checkgit log --onelinefirst. - Marking the first line of an interactive rebase as
squash. There is nothing above it to squash into, so Git stops with an error. - Using plain
--forceinstead of--force-with-lease.
Related reading
#Was this resource helpful?