1. Overview
In this tutorial, we’ll see how to modify a Git commit message, whether it’s the most recent commit or an older one.
2. Amend the Most Recent Commit Message
We’ll start with the easiest case. Let’s build a trivial commit that has a typo in its commit message:
$ touch file1
$ git add file1
$ git commit -m "Ading file1"
[articles/BAEL-5627-how-to-modify-git-commit-message 3e9ac2dbcd] Ading file1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1
Let’s now confirm the typo exists in our latest commit message, and also note the commit’s hash:
$ git log -1
commit 3e9ac2dbcdde562e50c5064b288f5b3fa23f39da (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <[email protected]>
Date: Tue Jun 21 21:53:12 2022 +0200
Ading file1
To fix the typo, we’ll use the amend option:
$ git commit --amend -m "Adding file1"
[articles/BAEL-5627-how-to-modify-git-commit-message 66dfa06796] Adding file1
Date: Tue Jun 21 21:53:12 2022 +0200
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1
Again, let’s display the most recent commit:
$ git log -1
commit 66dfa067969f941eef5304a6fbcd5b22d0ba6c2b (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <[email protected]>
Date: Tue Jun 21 21:53:12 2022 +0200
Adding file1
We can now confirm that the typo is fixed in the most recent message, but also note that the commit hash has changed. Technically, we have not changed our commit but replaced it with a new one.
3. Reword an Older Commit Message
Let’s now add two new commits so that the typo does not exist in the most recent commit but in an older one:
$ touch file2
$ git add file2
$ git commit -m "Ading file2"
[articles/BAEL-5627-how-to-modify-git-commit-message ffb7a68bf6] Ading file2
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file2
$ touch file3
$ git add file3
$ git commit -m "Adding file3"
[articles/BAEL-5627-how-to-modify-git-commit-message 517193e1e9] Adding file3
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 fil3
Let’s check the two commits we just added:
$ git log -2
commit 517193e1e99c784efd48086f955fcdbc3110d097 (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <[email protected]>
Date: Tue Jun 21 22:04:56 2022 +0200
Adding file3
commit ffb7a68bf63c7da9bd0b261ebb9b2ca548aa1333
Author: baeldung <[email protected]>
Date: Tue Jun 21 22:02:59 2022 +0200
Ading file2
Git’s amend option only works with the latest commit, so we can’t use that to fix the typo this time.
Instead, we’ll use rebase.
3.1. Start an Interactive Rebase
To fix the older commit message, let’s proceed with what is called an interactive rebase by running the following command:
$ git rebase -i HEAD~2
hint: Waiting for your editor to close the file...
HEAD~2 here means that we’ll be revisiting the two most recent commits.
This will open the text editor associated with Git on your machine and populate the editor with all the commands that can be used during a rebase procedure, including:
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
As we can see, we can do all sorts of things when rebasing commits, like collapsing two commits into one via the squash command.
Here, we want to rewrite the commit message, so we’ll use the reword command.
3.2. Reword the Commit Message
The two first lines in the editor contain the following text:
pick ffb7a68bf6 Ading file2
pick 517193e1e9 Adding file3
Note that, in this view, the commits are listed from the oldest to the most recent, as opposed to when we use git log.
Let’s change the first line to use the reword command instead of the pick command; we’ll leave pick for the second commit because we want to keep that message as it is:
reword ffb7a68bf6 Ading file2
pick 517193e1e9 Adding file3
If we wanted to change both messages in a single rebase, we could simply change the commands on both lines to reword.
Now, we haven’t changed the commit message just yet. So let’s save our file and close the text editor, which lets Git know we have finished our rebasing instructions.
Git will now process the rebase commands, prompting us when it needs our interaction. Since we told Git to reword the first commit, it’ll reopen the text editor with the contents of the first commit. The first line contains the commit message:
Ading file2
The comments on the following lines describe how the reword operation will work.
Let’s edit the commit message by modifying the first line to “Adding file2”, saving the file, and closing the editor.
Git will update our commit message and then finish with the rest of the rebase instructions. Let’s confirm Git’s work:
$ git log -2
commit 421d446d77d4824360b516e2f274a7c5299d6498 (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <[email protected]>
Date: Tue Jun 21 22:04:56 2022 +0200
Adding file3
commit a6624ee55fdb9a6a2446fbe6c6fb8fe3bc4bd456
Author: baeldung <[email protected]>
Date: Tue Jun 21 22:02:59 2022 +0200
Adding file2
Also, note that the commit hashes of these commits have changed, just like our amended commit before. All of our original commits have been replaced with new ones.
4. Pushing Your Rewritten Commits
At this point, we have three commits on our branch: an amended one and two rebased ones. They all have different commit hashes from our original commits.
As long as the original commits haven’t been pushed to any remote repository, we’ll be able to push normally.
However, suppose we had previously pushed any of our flawed commits before replacing them. In that case, the remote repository will refuse our new push because our local commit history is no longer compatible with the repository’s history.
Thus, if we want to push corrected (but previously pushed) commits, we’ll have to use the force option:
$ git push --force
This command should be used carefully because it will overwrite the remote branch with our changes, which can cause problems if the branch is being used by other people.
5. Conclusion
In this article, we’ve seen how to edit a commit message, be it the last one or an older one. We also saw how to push a changed commit to a repository that has the original one, noting that this should be done with care.