How to modify messages from unpushed commits

Posted on:
| Version Control
Picture

When working with version control tools like Git, it's not uncommon to make a typo or be unhappy with the message we've attached to a commit. Fortunately enough, there are two ways to modify the message of an unpushed commit in Git, depending on whether you want to change the message of the most recent commit or an older one.

Modifying the message of the most recent commit

It's as simple as running the command below, although it's important not to have any staged changes before running it to avoid unwanted results, as the --amend option also allows you to apply staged changes.

git commit --amend -m "New message"

The -m flag specifies the new commit message, replacing the old commit message with the one you specify inside the quotes. In the example above, this would be New message.

As an extra, if you just wanted to amend the most recent commit by applying your current staged changes without changing the commit message, you would run this:

git commit --amend --no-edit

Note that the -m flag and the quoted message have been replaced by the --no-edit flag, which tells Git to keep the existing commit message.

Modifying the message of a buried commit

Let's imagine we have the following 5 unpushed commits:

commit 6b7e2b3725370766e352ca02a50600d8dce49e4e (HEAD -> main, origin/main)
Author: HenestrosaDev <git@henestrosa.dev>
Date:   Fri Jul 5 18:23:56 2024 +0200
 
    Add localization tests
 
commit 8acdf3c2b7ced9c3e16541d7fdd94767e4e526d6
Author: HenestrosaDev <git@henestrosa.dev>
Date:   Fri Jul 5 18:20:33 2024 +0200
 
    Add Greek (el) localization
 
commit 2c2353c4b38d900f3bb79f888ae7a86463360d4b
Author: HenestrosaDev <git@henestrosa.dev>
Date:   Fri Jul 5 18:15:28 2024 +0200
 
    Add Portuguese (pt-BR) localization
 
commit 56e0f96184f2e82dac7e3210f33cec1dcbc55b14
Author: HenestrosaDev <git@henestrosa.dev>
Date:   Fri Jul 5 18:11:34 2024 +0200
 
    Add Spanish (es-ES) localization
 
commit 2fbb070383ea20b6d28821a359998eeec5b8e636
Author: HenestrosaDev <git@henestrosa.dev>
Date:   Thu Jul 4 18:00:04 2024 +0200
 
    Create test helper to find missing localized strings

If we wanted to modify the commit message from the July 4 commit, we would have to do the following:

  1. Interactively rebase the last 5 commits in the current branch by running this command:
    git rebase -i @~9
    After running it, Git opens an editor (Vim) showing the last 5 commits. Let's focus on this part:
    pick 2fbb070 Create test helper to find missing localized strings
    pick 56e0f96 Add Spanish (es-ES) localization
    pick 2c2353c Add Portuguese (pt-BR) localization
    pick 8acdf3c Add Greek (el) localization
    pick 6b7e2b3 Add localization tests
  2. Before delving deeper into what to do next, here is a basic guide to use Vim to continue with the process:
    • The arrow keys work fine in Vim, but it's more productive to use h (), j (), k (), l () in order to avoid moving your hand back and forth between the home row and the arrow keys.
    • i to insert text before the cursor -- enters Insert mode.
    • I to insert text at the beginning of the line -- enters Insert mode.
    • Esc or Ctrl+c to exit Insert mode and return to Normal mode.
    • u to undo the last change.
    • Ctrl+r to redo the last undone change.
    • yaw to yank (copy) the word under the cursor, including surrounding spaces.
    • yiw to yank (copy) the word under the cursor without surrounding spaces.
    • p to paste after current position.
    • P to paste before current position.
    • :wEnter to save (write) a file.
    • :qEnter to quit Vim.
    • :q!Enter to quit Vim without saving.
    • :wqEnter or ZZ to save and quit Vim.
  3. Go to the line pick 2fbb070 Create test helper to find missing localized strings and switch to Insert mode to replace pick with reword, like this:
    reword 2fbb070 Create test helper to find missing localized strings
  4. Exit the Insert mode by pressing Esc.
  5. Enter :wqEnter or ZZ to save and quit Vim.
  6. After that we will be prompted with as many editors as we have changed pick to reword. In this case, we will only be prompted with one editor, as we only want to change one commit message. Note that you can change as many commit messages as you like at once by applying the step 3 to multiple commits. Modify the commit message and enter :wqEnter or ZZ when you finish.

Canceling a rebase operation

If for some reason you change your mind in the midst of rebasing and don't want to edit your commit messages anymore, you can exit the Vim editor(s) with :q!Enter and then run git rebase --abort to stop the rebase in progress and return the repository to the state it was in before the rebase started.

Undoing a rebase operation

If you've already done the rebase, and afterwards realize that you want to undo the changes you made to one or more commit messages, you can run git reflog. It consists of a log of where your branches have been as maintained by Git. You can use it to find the state of your branch before the rebase and revert back to it.

After running it, look for the entry that corresponds to the state before you started the rebase. It should look something like this:

gde8542 (HEAD -> master) HEAD@{0}: rebase -i (finish): returning to refs/heads/master
ora9512 HEAD@{1}: rebase -i (start): checkout HEAD~4

Once you identify the correct commit hash (let's say it's ora9512), you can reset your branch to that commit by running git reset --hard ora9512. This will reset your branch to the state it was in before the rebase, effectively undoing all changes made during the rebase. Be cautious with git reset --hard though, as it will discard all changes in the working directory and index.