loading...
Cover image for How to correct Git Commit Messages

How to correct Git Commit Messages

devmount profile image Andreas Updated on ・3 min read

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"

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

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

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

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

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

Posted on by:

devmount profile

Andreas

@devmount

creator. developer. consultant. freelancer. javascript. php. python. css. husband. dad². guitarero. climber. retro gamer.

Discussion

markdown guide
 

While this is great advice to make changes locally.

Please don't ever change commits in the source repository. Even changing them in a MR/PR is highly undesirable, it makes it harder for your collaborators to understand what is going on.

 

Thank you for pointing out the difficulties for collaborators, totally agree with that. I will soon update this post to make it more clear!

 

Great reminders. The first one I find myself using a lot when I spot a typo or have missed out some info from the message. I'm glad you put side note 2 in - depending on what else is going on it is so easy to dig a deep hole using force pushing.

 

Thank you, totally agree with that!