The letter A styled as Alchemists logo. lchemists
Published February 15, 2025 Updated February 15, 2025
Cover
Git Rebase Reword

The ability to reword a single commit or multiple commits via the Git Rebase Editor saves a lot of time when using a Git Rebase workflow. This is also true when needing to edit the subject, body, and/or both at the same time.

To set the stage, let’s take a closer look at rewording commits by making a Git repository for demonstration purposes:

mkdir demo
cd demo
git init
touch README.adoc
git add .
git commit --message "documentation"
touch LICENSE.adoc
git add .
git commit --message "Added license"

If you run git log --oneline, you should see the following commits in your Git history:

3c9c0453cdf4 (HEAD -> main) Added license
f13ea68b463e documentation

There’s a problem. Our first commit, documentation, isn’t descriptive or uniform. Plus, we didn’t add a body to explain why the commit was necessary which is important for good Git Commit Anatomy. Let’s fix by interactive rebasing:

git rebase --interactive

You should now see the following in your Git Rebase Editor:

p f13ea68b463e documentation
p 3c9c0453cdf4 Added license

💡 I’m using Git Rebase Abbreviations which is why you see p instead of pick.

Now we can reword the incorrect documentation commit by changing p to r (short for reword) which means your Git Rebase Editor should look like this:

r f13ea68b463e documentation
p 3c9c0453cdf4 Added license

If you save and exit your editor, Git will start rebasing while stopping at the "documentation" commit in your editor. Now we can fix the subject and add a body as follows:

Added documentation

Necessary to explain what this project is, why it's imortant, and how to use it.

Much better because this allows us to explain what the change is and why the change is important per good Git Commit Anatomy. We can confirm by running git log --oneline to see our revised history:

c85d24913ace (HEAD -> main) Added license
57066e19f020 Added documentation

There’s a remaining problem, though. We didn’t explain why we added the license (even though the subject looks good). Let’s fix the license commit by rebasing. Before we do, you might be thinking that you can use git commit --amend to reword. You’d be correct. For learning purposes, I want you to perform a reword by fixing the commit instead. We can do this by using the license SHA (c85d24913ace in my case) like this:

git commit --fixup=reword c85d24913ace

This’ll cause the following to show up in your editor:

amend! Added license

Added license

This is a special directive for rewording commit messages (subject and body) only. So let’s add our missing body so the message looks like this:

amend! Added license

Added license

Necessary for legal purposes and accountability by our Legal department.

Upon saving and existing your editor and then running git log --oneline, you should see the following:

7283b9f902ef (HEAD -> main) amend! Added license
c85d24913ace Added license
57066e19f020 Added documentation

Notice the amend! directive. This instructs Git to apply our commit message changes upon our next rebase. Now we can run git rebase --interactive to apply these changes which will look like the following in your Git Rebase Editor:

p 57066e19f020 Added documentation
p c85d24913ace Added license
f -C 7283b9f902ef amend! Added license # empty

💡 I’m using Git Rebase AutoStash which ensures the amend! is next to the original "Added license" commit.

Notice the f (i.e. fixup) and the -C directive. This instructs Git to replace the original commit message with the contents of this message. Now you can save and exit your editor to see the following in your get log (i.e. git log --oneline):

b82ac13485d2 (HEAD -> main) Added license
57066e19f020 Added documentation

There is no change to the subject (as expected), but if you use git show, you’ll see the body as shown below:

commit b82ac13485d2aa71280cb03a553e5c25a85a2d68 (HEAD -> main)
Author: Brooke Kuhlmann
Date:   Sat Feb 1 13:39:23 2025 -0700

    Added license

    Necessary for legal purposes and accountability by our Legal department.

This is definitely more work than using git commit --amend but I want you to be aware of this workflow because this is especially helpful when you have multiple commits on your feature branch and want to amend/correct the commit messages with the intent of rebasing at a later point in time.

Now you can reword with grace. Enjoy!