git rebase -i HEAD~2 #assumed last two commits need to be squashed
Similarly HEAD~3 for squashing last three and so on
As a result vi
or your any other text editor opens up top two lines(because we are squashing two commits) will be
pick [commit-sha] [commit-message]
pick [commit-sha] [commit-message]
There will be more lines below them but all would be commented
Replace pick
with squash
and leave only one pick at the end, so that commit only will be visible and all squash commits will go inside that commit. For example
pick [commit-sha] [commit-message]
squash [commit-sha] [commit-message]
Save and close the editor.
Git will again fire up your text editor to change commit message this time.
Comment everything and leave or maybe modify the one commit message in the editor and then save and close.
Then simply verify if it went the way you want using git log
and then git push --force
to make similar commit history on remote.
there might be possibility of in between merge conflicts that needs to be resolved but that's separate story.