Site icon R-bloggers

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

[This article was first published on R | Dr Tom Palmer, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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, 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….

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. 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. However, what if you can’t easily or don’t want to reorder your commits. The proper answer is to perform an interactive rebase, however, I have a simple trick in GitHub Desktop to avoid this.

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:

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 press Shift+A.

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.

To leave a comment for the author, please follow the link and comment on their blog: R | Dr Tom Palmer.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version