Squashing Commits with Magit

While squashing Git commits isn’t too difficult, with Magit, I don’t want to use Git on the command line. However, manipulating the commit history in Emacs isn’t as straight-forward as other Magit activities, so I thought I would share my notes.

To explain this, I figure I need a story. While in Magit, hit the l (lowercase L) twice to bring up the log history summary:

magit-rebase-1.png

Notice the line the cursor is on was my ‘presentation’ commit, but I forgot to commit the images, so the latest commit on top contains those. Ideally, they should be one commit.

Game on.

Identify the Latest Commit

With the cursor on the latest commit line, hit the r i keys to start interactive rebase.

The resulting buffer window should look pretty familiar, as our destination commit is on top:

magit-rebase-2.png

Squash and Move

Move the cursor down to the latest commit (the one we want to squash into the earlier commit).

Hit M-p to move the line up (back in time) so that it is just below the commit we want to meld this into.

Hit s to set the Squash action on this commit. Your buffer would look like this:

magit-rebase-3.png

Combine the Commit Messages

Hitting C-c C-c will bring up the two commit messages so that you can make single commit message tidy:

magit-rebase-4.png

In my case, I can just delete the one I don’t want:

magit-rebase-5.png

(Why yes, in this example I did ignore accepted convention and just used a single line commit message. Yes, I do feel the shame).

Hit C-c C-c when complete.

Summary

Pretty simple and quick approach to end up with this better history:

magit-rebase-6.png

What I like even move is the fact that using Magit actually leaves less room for error.