How to squash last X commits after they have been pushed

Zafer Ayan
2 min readMar 4, 2022

Sometimes you push your changes in PR and code owners may want you to squash commits. So how we squash commits that have been pushed?

First of all, we need to rebase last commits. For example let’s say we want to squash 3 commits as one:

git rebase -i zafer/fix_bug~3 zafer/fix_bug

And we need to pick first commit and squash others:

pick e14fefe3 Fix bug on Safari
squash 743e3b47 Fix tests
squash 137ecdfd Fix wrong naming

And after that, we need to write commit message according to our commit. For this purpose, we can comment last two lines with # operator:

# This is a combination of 3 commits.
# This is the 1st commit message:
Fix bug on Safari# This is the commit message #1:# Fix tests# This is the commit message #2:# Fix wrong naming

And after we save and close file, we can push our commit forcefully to replace remote commits with local ones:

git push --force

And thats it. We successfully squashed and pushed our commits.
See you in my next post ✌️

--

--