Open Source SoftwareTutorials

How to change git commit message text

A while ago we published an article how to get started with the git command.

Here's a small follow-up post, which might come in handy … when it happens.

git

Mistake in the git commit message

You have added your changes in your local git repository and are ready to push the changes back to the remote repository.

$ git commit -am "Ticket #107494: Fixing host variable"

And this is when you realize: Oh noo, I mentioned the wrong ticket number! It should have been #107492! *facepalm*

–amend to the rescue

The git commit command supports additional parameters. One of them is the –amend parameter. This allows you to re-edit your previous commit:

$ git commit --amend

This opens up an editor inside your terminal. Depending on your $EDITOR variable, this could be nano, vim or something else. If you want to change the editor, you can set the $EDITOR variable again:

$ export EDITOR=vim

The editor opens and shows your last commit message, which you can now change inside the editor. Once you have edited the text, save and exit the editor (:wq in vim). The output in the terminal will show your new commit message:

[master 66c4fe52] Ticket #107492: Fixing host variable and reference
 Date: Fri Aug 18 13:59:59 2023 +0200
 2 files changed, 2 insertions(+)

You can now push your commit(s) to the remote repo and continue working normally, as if nothing had happened.

$ git push 

You can verify the adjusted commit message in the git log history:

$ git log|head -n 1
commit 66c4fe52e5d6db6186050c8cad6d611947df53bc

$ git show 66c4fe52e5d6db6186050c8cad6d611947df53bc
commit 66c4fe52e5d6db6186050c8cad6d611947df53bc (HEAD -> master, origin/master, origin/HEAD)
Author: Developer <dev@example.com>
Date:   Fri Aug 18 13:59:59 2023 +0200

    Ticket #107492: Fixing host variable and reference
[...]

Note: git commit –amend only works BEFORE you do a git push. Otherwise your commit message was already pushed to the server.

Claudio Kuenzler
Claudio has been writing way over 1000 articles on his own blog since 2008 already. He is fascinated by technology, especially Open Source Software. As a Senior Systems Engineer he has seen and solved a lot of problems - and writes about them.

You may also like

Leave a reply

Your email address will not be published. Required fields are marked *