Its always good to add a proper message with each git commit. These commit messages help you and other users to understand the purpose of any specific commit. Sometimes you add a wrong commit message and want to edit commit message.
This tutorial help you to how to change last commit message in git repository.
Change Git Commit Message
Here you may have 2 situations. One you may have pushed your commit to remote repository or second you have not pushed your commit.
Follow the below instructions based on your pushed commit or not.
I have not Pushed Commit
We can update last commit log using --amend
option in git commit. Use the following command to do it but remember that if some other has already sync your changes then they again need to sync your code to avoid any future problems related to this.
git commit --amend -m "Your updated commit message"
This will change the last commit message in your local repository.
I have Pushed Commit
Okay, no problem here, You can still update commit message. But its not recommended option to change message of pushed commit. In this situation, you have to push your changes again to remote server forcefully.
First, change the commit message on your local repository.
git commit --amend -m "Your updated commit message"
Then push your changes to remote origin with --force
option.
git push origin master --force
Here we assume, that origin
is your remote repository and your are pushing changes to master
.
Conclusion
The conclusion is that use the git commit –amend command to change message of your last commit.
Hope this tutorial helped you to change git commit message. If you found any better solution to handle this situation, Please add a comment below.