How to change the last commit message in Git?

Published February 17, 2021

To change the last commit message in git, you can add the --amend flag to the git commit command along with the new commit message.

# Change last commit message
git commit -m "New commit message" --amend

This will only change the most recent or the last commit message.

To push the new commit to the remote branch or the repository you have to force push the commits using the --force flag in the git push command, this is because the commit id of the newly changed commit message (or the SHA signatures) will be different. To do that you can do like this,

# Change last commit message remotely
# by force pushing the commits
git push origin branch-name --force

Caution: Force pushing the commits may make some inconsistencies if the same branch is being used by other users.

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter