How to Change GIT Commit Message


Amend Last GIT Commit Message

To amend a Git commit message, you can use the git commit --amend command. This command allows you to edit the previous commit message and update it with a new message.

git commit --ammend
git commit -ammend -m "you message"

Here are the steps to amend a Git commit message:

  1. First, make any changes to the files in your Git repository that you want to include in the commit.
  2. Run git add to stage the changes. For example, if you want to stage all changes, you can run git add ..
  3. Run git commit --amend -m "new message" command. Replace “new message” with the updated commit message you want to use.
  4. Save and close the commit message editor.

This will update the commit message of the most recent commit in your Git repository.

Note that if you have already pushed the original commit to a remote repository, you should not use git commit --amend, as it changes the commit history. Instead, you should create a new commit with the updated message using git commit -m "new message", and push it as a new commit to the remote repository.

Amend GIT commit message with rebase

you can amend a Git commit message using rebase. Here are the steps to do so:

  • First, you need to find the commit that you want to amend. You can use the git log command to see the commit history and find the commit that you want to amend. Note down the hash value of the commit.
$ git log
commit c48a14b0d7e57f919a1e0a4d4b4f7ee4f9a0a7de (HEAD -> master)
Author: John Doe <[email protected]>
Date:   Tue Apr 20 10:00:00 2021 -0400

    Added new feature

commit 234d4e1c4e4b2f24c9ed2947f9208b237cc1f12c
Author: John Doe <[email protected]>
Date:   Mon Apr 19 09:00:00 2021 -0400

    Updated README
  • Use the git rebase command to change the commit message. You can use the interactive mode to edit the commit message. In the following example, we will amend the commit with the hash value c48a14b0d7e57f919a1e0a4d4b4f7ee4f9a0a7de.This will open an editor with a list of commits starting from the commit you specified. In this case, it will list only one commit.
$ git rebase -i c48a14b0d7e57f919a1e0a4d4b4f7ee4f9a0a7de^
  • Change the word “pick” to “reword” in the line that corresponds to the commit you want to amend. Save and close the file.
pick c48a14b Added new feature

Change it to:

reword c48a14b Added new feature
  • Save and close the file. This will open another editor with the commit message for the commit you want to amend. Edit the message as desired.
  • Save and close the file. Git will apply the changes and amend the commit message.
  • Use the git log command again to verify that the commit message has been amended.