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:
- 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
-
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"
-
Drag and drop the empty commit onto your target commit. See the screenshot at the
top of this post. -
Enter your amended commit message and delete the text in the Description box.
- 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).
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.
Related