This post will show you how to change the git commit message. And this commit could be the last one or a specific number of commit such as the 5th or 6th or 9th commit message.
You’ll find all sorts of documents to change the commit message. Such as every step with exact commands, screenshots, and video instructions.
How to change the last git commit message?
If you wrote a wrong message in the last commit, you can change it very easily as you normally run other git commands.
To change the last commit message, type the following command & hit enter:
git commit --amend -m "Your desired new message goes here"
After that, you can push the progress to the remote origin (GitHub, Bitbucket, etc). If you find any fatal error when pushing, use the following command to get around:
git push origin master --force
However, make sure that you’re not overriding someone else’s work with the
An alternative way to change the last commit message
There is another method to change the last commit message and it’s very similar to the first (above) one.
Type the following command and hit enter:
git commit --amend
The above command will show you the last message in a prompt. To edit the message to the following step-by-step:
Step 1: Press the “Esc” key on your keyboard. (nothing will happen after you press)
Step 2: Next, press the “i” key. Now you will able to edit the last committed message.
After you edited your message, do the following mentioned below:
Step 1: Press the “Esc” key again.
Step 2: Press “Shift + zz” (the “Shift” key & the “z” key twice). This will amend the message.
How to change the last commit message that has been pushed to the remote origin such as GitHub or Bitbucket repository?
You can follow the same process to change the last commit message. But if you already pushed the last commit, you will find a fatal error when trying to push the edited commit.
To get around this error, type the following command and hit enter:
git push origin master --force
This will also overwrite the commit message to the remote origin such as GitHub, Bitbucket, etc.
How to change a specific git commit message?
In the previous section, you learned how to edit the last commit message. But there are some scenarios where you may need to change a specific commit message such as the 5th or 9th or etc.
And in this type of case, the
git commit --amend will not work and you have to use rebase instead. Let’s follow me to edit a specific message.
In this example, I will edit the 3rd commit message. But you can edit any message using these steps.
In your command line, type the following command and hit enter:
git rebase -i HEAD~n Here “n” is the specific number you want to go back to. In my case, it’s 3 because I will change the 3rd commit message. So, my command will be as follows:
git rebase -i HEAD~3
It will show you the last 3 commit messages (based on my example) in reverse order (as you see in the above screenshot). But you are not able to edit it yet.
However, I went up to the 3rd message but I don’t need to edit the rest of the commit messages. So I will leave the rest of the messages untouched.
Since the commit list shows in reverse order, I only need to change the first one (from the top of the list). And it will be the same for you if you want to change only one commit message. No matter how many commits you do need to go back (n).
Anyways, next, type/press the “Esc” key on your keyboard.
Then type/press the “i” on your keyboard.
Replace the word “pick” with “edit” in the first message.
Next hit “Esc” and then “Shift + ZZ”
git commit --amend
You will be prompted to edit the exact message as you see in the screenshot below.
Again press the “Esc” and then “i” and edit the message.
Once you finish editing, hit “Esc” and “Shift + ZZ” again.
git rebase --continue and hit enter.
To push it, type
git push origin master --force and hit enter.
However, use the
--force flag with caution. For more detail, see how to push after rebase.
This is how you can change a specific commit message both locally & the remote origin (repository).
After pushing it to my GitHub repository, it successfully changed the 3rd commit message as you see in the screenshot below.
I know this is intimidating and hard to follow. But this is how it works.
There are other scenarios when you need to use git rebase such as to clean up commit history, including changes from the parent branch, solve merge conflicts, etc. For more details, see when you should use git rebase.
Some of you may have issues following along. So I thought it would be better if I showed everything in a video. The video below explains & shows everything step by step that I mentioned in this post.
Learn more about Git
- What is Git and why it is used?
- How to install Git on Windows & Mac?
- How to use Git with Visual Studio Code?
- How to use Git and GitHub?
- What is the difference between Git and GitHub?
- How to connect local Git to a remote server?
- How to change Git remote origin URL?
- How to create a Git repository on GitHub?
- Git add all (stage all changes)
- How to undo the git add -A command?
- How to undo the last Git commit?
- How to change the git commit message?
- How to remove or unstage a file from Git commit?
- How to git push after rebase?
- Git switch branches: How to checkout the git branch?
- How to merge a git branch to master?
- Git branch: create, checkout, merge, list, command, delete & push
- How to clone a Git repository?
- How to git pull to override the local project?
- How to remove file from Git?
- When should you use git rebase?
- Git commands & explanation (downloadable cheatsheet included)
- How to create GitHub Pages?
Following this guideline might be hard especially if you’re a beginner. Also, changing a commit message on a deeper level is a bit trickier than the last one. However, once you become familiar with it and after you successfully rebase the commit message, it will be easier for you the next time.
It was also hard for me to walk you through the process and document everything. In real life, it only takes a minute or two to change any commit message but documenting everything step by step is hard.
Anyways, if you still have issues, let me know.