Many times in Git, we commit some work only to realize that is a mistake, and we should do another way. The easy way to fix that is to revert the previous commit, a process in which Git creates another commit that undoes exactly everything in the last commit. After that, we move on with the other way and check in commits for that. Before pushing everything to the remote branch, as responsible software engineers :), we sometimes want to “squash” the commits to erase the mistake to keep the commit log clean.
In the example shown below, my commit
daefc6e was a mistake, and I reverted it with
f3886c2 commit, and then I checked in my correct solution in
I wanted to squash those commits in an interactive rebase session, as seen in the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
git rebase always fail in such situations with the following “error” message:
1 2 3 4 5 6 7 8 9 10
The error message is very confusing.
f3886c2 commits are squashed, the net effect is nothing, which is the “empty commit” mentioned in that error message.
However, retrying the
git rebase command with
--allow-empty as said does not work.
git rebase --continue does not work as expected: it does not squash three commits into one.
After some Google searching, it turns out that the above error message comes from
git commit --amend, which is delegated by
git rebase to handle the squash.
When the message says “repeat your command”, it means repeating the
git commit --amend command, something would never occurs to us.
Therefore, the right thing to do here is repeat
commit and continue with the interactive rebase session:
1 2 3 4 5 6 7
By doing that, we will now have all three commits squashed into one and help cleaning up the commit log.