Squash commits keeping the original message

Let’s say you added the baisc code for the story you’re working on with a simple message of “enable sso,” then you pushed it.

A few moments later, you realised a typo, so you pushed a fix of this typo with another message of “fix typo” and when your peer started reviewing your PR, he/she pointed out that there were some unnecessary files that needed to be cleaned out.

Finally, you pushed a third fix of the clean out ask, now you want your PR itself to look clean and consolidate those 3 commits in the original one.

The commits respecticly are:

  1. enable sso
  2. fix typo
  3. clean out

The commit tree look like this

gitGraph commit id: "some other feature" branch feature/enable-sso commit id: "enable sso" commit id: "fix typo" commit id: "clean out"

Execute the following rebase commands in your local command shell or terminal

1
git rebase -i HEAD~3

An interactive screen will open in your default editor; I use vim, but yours may differ. At the interactive screen, choose fixup for commits 2 and 3, by adding f in front of those commits, while leaving the pick in front of the main commit.

You can now push your branch by including a leading plus before the branch name, as below

1
git push -u origin +feature/enable-sso

Here is how the commit tree look like after rebase

gitGraph commit id: "some other feature" branch feature/enable-sso commit id: "enable sso"
updatedupdated2024-01-172024-01-17