How to squash last X commits after they have been pushed

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 ✌️

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Crodo — finding new stars in the Cronos Network ecosystem (PT.4)

The Early Velocity Trap

Picture of a mouse looking for an escape route in several traps.

How To Rewrite URLs with mod_rewrite for Apache on Debian 8

#DesignNotes: Draw.io

How to Add a CSS Gradient Overlay to a Background Image

Example of an image with some example text displayed on top.

[6]OV Exclusive — XGRAV

Advanced debugging with gdb (Reverse execution , Pretty-Printer and Asan)

Night light with Redshift

A light bulb reminiscent of Redshift’s symbol

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Zafer Ayan

Zafer Ayan

More from Medium

Common response strategies for production errors in backend systems

MicroServices Made Simple with Moleculer

Firebase Auth: Sign In Link with Custom Expiration

What is JWT