Edit, change or amend the last Git commit message
Developers often commit to their local Git repository only to realize the associated commit message isn't up to par.
Fortunately, it's easy to edit and change the last Git commit message, although development teams should be aware of some dangers, especially if the last Git commit has already been shared with others.
How to amend a Git commit message
Use the amend option of the commit command to quickly change the last Git commit message.
The full command to update and amend a Git commit is as follows:
git commit --amend -m "change last Git commit message"
The git commit --amend command appears straightforward on the surface, but it doesn't quite work the way a developer might expect.
The git commit --amend command
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(-)
Inspecting an edited Git commit
If a developer uses the git reflog command to query the local commit history, the response shows 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 happens in the Git version control system.
Don't push a Git commit update
If a developer attempts to use the git log command to view the public history of a local repository, there won't be any evidence of the historic trail of commits.
In this scenario, a git log command only shows evidence of one commit, 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
The history documented in the Git log is what's replicated when a developer pushes 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 looks at the local repository using anything other than the local reflog command, it appears as though the last Git commit message changed.
Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development, DevOps and container-based technologies such as Docker, Swarm and Kubernetes.