Get started Bring yourself up to speed with our introductory content.

How to change the last Git commit message quickly

Developers often save an update to a local Git repository and then realize that the associated commit message isn't up to par. Fortunately, it's possible in Git to change the last commit message. But DevOps teams should be aware of some caveats associated with the process.

Let's explore how to use the --amend command and how it can change the last commit message.

How to change the last Git commit message

The following command allows a developer in Git to change the last commit message:

git commit --amend -m "change last Git commit message"

The command appears straightforward on the surface, but it doesn't quite work the way a developer familiar with Git might expect.

The git commit --amend command in action

The first nuance of the git commit --amend command is that it doesn't technically change the last Git commit message. Instead, it creates a new commit identical to the prior one, except for the amendment. For example, look at the following scenario in which the developer issues a commit against a repository and then changes the last Git message with the amend switch:

$ git init
Initialized empty Git repository 

$ touch index.html
$ git add .
$ git commit -m "Git commit message to change"
[master (root-commit) bc78354] Git commit to change
1 file changed, 0 insertions(+), 0 deletions(-)
$ git commit --amend -m "This is how to change the last Git commit message"
[master 2f2fe99] This is how to change the last Git commit message
1 file changed, 0 insertions(+), 0 deletions(-)

If a developer uses the git reflog command to query the local commit history, the response will show two separate and distinct commits:

$ git reflog
2f2fe99 HEAD@{0}: commit ( amend ): This is how to change the last Git commit message
bc78354 HEAD@{1}: commit (initial): Git commit to change

The message for the first commit (bc78354) hasn't changed. The line bc78354 retains its original commit message. Git created a new commit message with its own unique identifier (2f2fe99) with the amended message.

The amend process creates the illusion that the Git commit message changed, but that's not what actually happens in the version control system.

Change last Git commit message
How reflog appears after you change the last Git commit message.

Push an amended Git commit message

If a developer was to use the git log command to view the public history of a local repository, there wouldn't be any evidence of the historic trail of commits. In this scenario, a git log command only shows evidence of one commit having occurred, and that commit contains the updated message:

$ git log 
commit 2f2fe99eaf8da88a6dc90c45f49815e9b7fb32aa (HEAD -> master)
Author: Me [email protected]
    This is how to change the last Git commit message

Also, note that the content of the Git log is replicated when a developer pushes it to a remote repository such as GitHub or GitLab. That means no public evidence exists to indicate that any other commits happened.

In the end, the net effect of the git commit –amend command is that the last commit message appears changed, without any evidence of a commit with the old commit message.

If anyone were to look at the repository using anything other than the local reflog command, it would appear as though the last Git commit message changed.

View All Videos
App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close