How to Update Git Commit Messages

Have you ever been in a situation where you press the Enter key after typing git commit -m "Commit message" and then realize that you made a mistake in your commit message or could have structured it in a better manner?

Keep reading this article to understand how to update your Git commit messages.

The set of commands needed depends on:

  • The state - whether the commit is recent or not
  • The host - where the commit resides (local or remote repository)

Recent Git Commits

The --amend command is useful for changing recent commits.

If you are yet to push the code to the remote repository, use:

git commit --amend -m "New commit message"

git push origin <BRANCH_NAME>

However, if you have pushed the code to the remote repository, use:

git commit --amend -m "New commit message"

git push --force-with-lease origin <BRANCH_NAME>

Some people use --force but it is not advisable to do so; --force overwrites a remote branch with your local changes however, --force-with-lease do not overwrite the remote branch if more commits have been pushed to it by another contributor; the push fails and you would be able to pull the changes before attempting to push again.

Previous or Multiple Git Commits

Use git rebase when you need to update the Git commit message of earlier or multiple commits. Assuming you want to update the Git message of any/all of the last 3 commits, you can use the interactive Git rebase.

To do this, navigate to the branch and type:

git rebase -i HEAD~3

This will autostash your changes and in this scenario show the last 3 commits.

pick 62189aj9 Introduce usecase (#120)
pick 71f3n138 Write an example (#121)
pick b5gkwd2g Explain rebase (#122)

# Rebase ge816a67..b5gkwd2g onto ge816a67 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label

To commence updating the messages:

Press i to enter an interactive mode. Navigate to the lines you want to update using your arrow keys and change pick to reword

pick 62189aj9 Introduce usecase (#120)
reword 71f3n138 Write an example (#121)
reword b5gkwd2g Explain rebase (#122)

Press esc to leave the interactive mode.

Then, press SHIFT + : and type wq to write your changes and quit the editor.

An editor will be displayed to enable you update the commit message of each selected commit - one at a time. Based on the previous choice, this will list the last 3 commits and pre-load the message you need the update:

Write an example <!-- Change the message to what you want -->

- Introduce usecase

- Write an example

- Explain rebase

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Date:      Wed Mar 9 21:51:30 2022 +0100
#
# interactive rebase in progress; onto ge816a67
# Last commands done (2 commands done):
#    pick 62189aj9 Introduce usecase (#120)
#    reword 71f3n138 Write an example (#121)
# Next command to do (1 remaining command):
#    reword b5gkwd2g Explain rebase (#122)

Push the updated changes:

git push --force-with-lease origin <BRANCH_NAME>

Asides from using the rebase command to update commit messages, it can act as a cleaner alternative to the merge command when integrating changes from another branch.

Should you merge or rebase? I've been using merge for a long time but since I understood how rebase works, it became my preferred option. It helps maintain a clean log as merge commits aren't added as part of the commit history.

Conclusion

Whether you need to update a recent commit using git commit --amend -m "New commit message" or an older commit message using git rebase -i HEAD~n, this article explains the approach to achieve your aim.

Article Tag  Workflow Tooling

Share this article:

Stay Updated

    More Articles


    Author
    Chiamaka Ikeanyi.

    I write about accessibility, performance, JavaScript and workflow tooling. If my articles have helped or inspired you in your development journey, or you want me to write more, consider supporting me.