How to correct Git Commit Messages

Andreas - Oct 10 '19 - - Dev Community

Who doesn't know it: You have to finish your work for some reason and quickly do a git commit to realize right after that you have a typo or something missing in your commit message. Or even worse: You realize it after pushing to remote...


I'm a fan of a leading emoji in commit messages. That way, I have a visual classification of commits in the git commit log and can easily identify bug fixes, additions or dependency updates. To be compatible to terminal output, I use the shortcodes (i.e. :key: for commits dealing with security) rather than inserting the emojis directly (馃攽). You may call me fussy, but I think the commit or file list looks really ugly, if you have a typo in an emoji shortcode:

GitHub commit typo

Fortunately, there are ways to correct a commit message afterwards. Let's take a look:

Correct the most recent unpushed commit message

This is the easiest one. Simply type the following:



git commit --amend -m "correct commit message"


Enter fullscreen mode Exit fullscreen mode

If you now push the changes to remote, the corrected commit message will appear.

When using VS Code, you can undo the last commit from the 路路路 menu:

Undo the last commit in VS Code

Or simply open command prompt with CTRL+SHIFT+P and type "undo".

Correct the most recent pushed commit message

If you already pushed the last commit, you can correct it the same way - but you will have to force push over the wrong commit:



git commit --amend -m "correct commit message"
git push --force


Enter fullscreen mode Exit fullscreen mode

Correct older commit messages

This one is a little bit more tricky. You need to find out, how many commits you have to go back, to find the one you want to correct. With that information you can use



git rebase -i HEAD~n


Enter fullscreen mode Exit fullscreen mode

to display the last n commits in your default text editor. This would be the output for the last 3 commits:



$ git rebase -i HEAD~3
pick da47737 :x: remove outdated entry
pick a27d81a :shirtt: clean linter warnings
pick f1db3c9 :hammer: fix missing condition

# Rebase 0ceed9a..f1db3c9 onto 0ceed9a (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out


Enter fullscreen mode Exit fullscreen mode

Now replace the word pick with reword before each commit message you want to correct, i.e.:



$ git rebase -i HEAD~3
pick da47737 :x: remove outdated entry
reword a27d81a :shirtt: clean linter warnings
pick f1db3c9 :hammer: fix missing condition


Enter fullscreen mode Exit fullscreen mode

Save the commit list and correct the commit message in each following commit file. As before you have to force push your changes.

Now after correcting the wrong emoji shortcode, I can find peace again when looking at my commit log. 馃槑

Important side notes

  1. Commit messages belong to the commit itself. So changing it means creating a new commit and replacing the one with the wrong commit message.
  2. Force pushing is not recommended, because you're changing the repository history. This means the local history of already existing clones of your repository has to be fixed manually, making contributions more difficult for collaborators.

Edited: 19th October 2019 (add VS Code example, add to side note 2)
Originally published: 10th October 2019 on Medium

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .