[Git] Modify last commit message

2022-10-27 hit count image

Let's see how to modify the last commit message with Git amend option.

Outline

When you use Git for version control, sometimes you commit with the wrong commit message. For this, Git provides the amend option to modify the last commit message.

In this blog post, I will introduce how to use the Git amend option to modify the last commit message.

git log

First, execute the git log command to get the current Git status like the following.

git log

After then, you can see the commit message like the following.

commit 3088f6f4174174d5aefa7ab65a9d8f85640766e3 (HEAD -> main)
Author: dev-yakuza <[email protected]>
Date:   Tue Oct 25 14:43:39 2022 +0900

    Add test.txt file

Modify and commit

Next, let’s modify the specific file, and execute the git commit with the wrong message like the following.

# git add .
git commit -m 'wrong message'

After committing the wrong commit message, execute the git log command like the following to check the commit log.

git log

After then, you can see the wrong commit message like the following.

commit 681d07fc09f74070ea480a610605e771d708ed4e (HEAD -> main)
Author: dev-yakuza <[email protected]>
Date:   Tue Oct 25 14:47:16 2022 +0900

    wrong message

git commit –amend

If you commit with the wrong message like this, you can modify the message by executing the git commit --amend command like the following.

git commit --amend

After executing the command, you can see the vi editor and can modify the message.

wrong message

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Oct 25 14:47:16 2022 +0900
#
# On branch main
# Changes to be committed:
#       modified:   test.txt

After modifing the message in here, execute the git log command to check the mssage is changed well.

git log

If you don’t have any problem, you can see the wrong commit message is modified well like the following.

commit 8002379053b52c0dba4e94bc734bd158ce83d695 (HEAD -> main)
Author: dev-yakuza <[email protected]>
Date:   Tue Oct 25 14:47:16 2022 +0900

    correct message

git commit –amend -m

You can use the git commit --amend command to show the editor to change the wrong message, but you can also modify the wrong message directly by executing the following command.

git commit --amend -m 'modify message directly'

After modifing the message, execute the git log command again.

git log

After then, you can see the message is changed well like the following.

commit 11a16aa7f686e34d2d71c078460ef743783652c4 (HEAD -> main)
Author: dev-yakuza <[email protected]>
Date:   Tue Oct 25 14:47:16 2022 +0900

    modify message directly

Completed

Done! We’ve seen how to use the Git amend option to modify the last commit message. From now, try to modify the typo or wrong commit message by using the amend option when you commit the code.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts