Amending the Git commit message of a previous commit (that isn't the most recent) in GitHub Desktop without performing an interactive rebase

A trick with an empty commit!

Screenshot of dragging an empty commit onto the commit whose commit message you want to amend in GitHub Desktop.

Introduction

As R developers I think we can all agree that Git is hard. There won’t be many of us who at some time haven’t broken a Git repository in some way or other or lost some work, I know that I have (several times … ahem).

A task I sometimes need to achieve when working on a branch is amending a commit message. I use GitHub Desktop to help me with Git, and I recommend it to all my students. If the commit you want to amend the message of is the most recent commit you can simply right click on it and select Amend Commit….

Screenshot of amending a commit in GitHub Desktop.

This is providing a user friendly interface to running

git commit --amend

in the terminal for us. This is all covered in the GitHub documentation.

However, what if the commit is not the most recent – when you right click those commits the option to amend them is not available. If your commits after your target commit don’t touch the same lines in the same file/s you could reorder your commits such that your target commit is the most recent and then right click and Amend Commit… again.

What if you can’t easily or don’t want to reorder your commits? At this point you might start questioning your life choices; how did I end up using the same tools as the Linux kernel developers? All I want to do is amend a commit message! Anyway, the proper answer is to perform an interactive rebase, but that is relatively advanced and not without stress. Luckily I have a simple trick in GitHub Desktop to achieve our goal without performing an interactive rebase.

The trick: squashing an empty commit onto the target commit

GitHub Desktop allows us to squash to commits together. When it does this it allows us to amend the commit message of the resulting commit. Therefore, to achieve our goal of amending previous commit messages we need to:

  • Identify the commit you want to amend the message of. Here I have made a typo and want to fix the message to say Use test-rcpp.R
Screenshot of squashing commits GitHub Desktop.
  • Create an empty commit

    For this you will need command line Git installed (GitHub Desktop has a version of Git bundled within it, so not everyone who has GitHub Desktop installed has Git installed separately). Run the following (you don’t have to include the message).

    git commit --allow-empty -m "Empty commit for purposes of trick"
    
  • Perform a squash in GitHub Desktop by dragging and dropping the empty commit onto your target commit as per the screenshot below (and in the screenshot at the top of this post). The empty commit has no content, so it does not affect the content of your target commit.

Screenshot of dragging the empty commit onto the target commit in GitHub Desktop.
  • Enter your amended commit message in the Summary box and delete the text in the Description box.
Screenshot of squashing commits GitHub Desktop.
  • Click Squash 2 Commits.
Screenshot of finalising squashed commit in GitHub Desktop.
  • That’s it, we’re finished! You can now push your branch upto GitHub (or in my case in the screenshot force push because I had previously pushed this branch to the remote).
Screenshot of your amend Git history ready to the pushed to GitHub in GitHub Desktop.

The proper method: performing an interactive rebase

If you want to do achieve this the proper way or amend the contents of previous commits you’ll need to perform an interactive rebase. That is a little bit tricky to perform in the terminal, although there are lots of helpful YouTube videos and blogposts showing how to do it.

If you ever need to do this I recommend using the Lazygit terminal user interface, which has the best interface to interactive rebasing I’ve seen. To start the process, navigate to the Reflog pane (by pressing Tab twice), then use your up and down arrows to select your target commit, and either press r to reword (i.e., amend the commit message) or e to edit the commit itself (or choose one of the other options listed at the bottom).

Screenshot of starting to amend a commit message in the Lazygit TUI.

Summary

I have shown how to amend commit messages for commits that aren’t the most recent commit in GitHub Desktop without performing an interactive rebase.

Tom Palmer
Tom Palmer
Senior Lecturer in Biostatistics applied to Genetics

My research interests include statistical methods for epidemiology.

Related