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:


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:


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:


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:


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


(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.


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


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

Date: 2014 Dec 23

Created: 2022-12-03 Sat 11:10