Search code examples
gitgit-rebase

Can I squash together multiple commits and keep the message from the last one?


Sometimes I git rebase -i master and have this situation:

pick cc1ed9d First commit
pick 9d4090c Main commit with detailed message
pick fca9df4 WIP afterthought commit with bad message

By changing the last line's pick to fixup, I squash it into the main commit, and the new, combined commit uses the main commit's nice message.

But sometimes the order is different:

pick cc1ed9d First commit
pick fca9df4 WIP preliminary commit with failing test
pick 9d4090c Main commit with detailed message

Here I'd like to squash together the last two commits but keep the message from the last one.

I can do this with laborious copy and paste, but is there a simple way?


Solution

  • You can slightly change what you have been doing. If you change pick (a.k.a. p) to squash (s) instead of fixup (f), then Git will combine the commits' changes in the same way, but instead of automatically retaining the 1st commit's message will bring up your text editor. Therein will be a proposed commit message, which concatenates the messages of all the consecutively squashed commits, from which you can edit down to whatever you want to keep.

    This does not 100% automatically retain only the message of the last commit, to be fair, but it sure avoids the mentioned "laborious copy and paste"!

    Think of fixup as squash without the extra step of editing the message, basically, so by changing from fixup to squash you get the full squashing experience (lol).