How to change the git commit message?

August 11, 2023

There are times when we need to change the git commit message, some times, after push and some times before push. This is happen when we make a typo in the message or want to remove some info from the message. We can change those commit message in different way depending upon the situation. Leyt's Move on.

Rename unpushed most recent / Last commit message

To make change in the last or most recent commit run the following command in the terminal.

git commit --amend

with this command the message file will open. and on the top of the file you will be able to see the message. just make the change according to your need. after make the change save the file.

(if the file open in nano press CTRL + X and enter to save the change.)

single command approach

We can also use the single command approach to change the most recent / last git commit.

git commit -amend -m "Your message"

write this command and within the quote add your new message. This will replace the old message with the new one.

Rename pushed commit message

Although it is not recommended to rename the pushed commit message, as it might cause some problem. But if we need to make change in the last or most recent pushed commit message we can use the following command.

git commit --amend -m "New commit message."

now push to update the history of the repository with the following command.

git push --force <remoteName> <branchName>

Thank you

Follow Me