Squash the first two commits with Git

Squash the first two commits with Git

With a normal interactive rebase, that cannot be done, you cannot include the first commit. That is, if you have three commits and wanna do a

git rebase -i HEAD~3

, Git won’t allow that. It will answer with something like

fatal: invalid upstream 'HEAD~3'

If you say HEAD~2, the rebase does not go far enough, you will still have two commits after that.

But, there is a new option (from Git 1.7.12 onwards, if Stack Overflow is right), --root. Use it like this:

git rebase -i --root master

Of course, this also works with more than two commits. So the heading should better be Include the first commit of a repo into interactive rebase.

But the situation where I came up with the question was squashing the first two commits. Often you start sketching and commit some intermediate, crappy, state, so your work does not get lost. When you got your first working solution, you commit again. To look good to the afterworld, you would like to hide the first commit by squashing it with the others. This is a common situation[1], so I’ll keep that heading.


1. At least for me.

links

social