Have you ever pushed one commit, and now thingking that the commit message you added was not correct and wanted to change the message ?
So, to tackle this problem we will consider two cases
- If commit is not pushed to remote repo
- If the commit has been pushed to remote repo
Changing the commit in local repo
To change any commit message we use --amend
flag with git commit
git commit --ammend -m "New Commit Message"
Now if you do git log
, you will see the last commit message to be updated by your new message.
Pushing the new commit
If you try to push normally the new commit message, you will get one error of Updates were rejected because the tip of your current branch is behind
It is because, before pushing, git will check status of your remote repo, and sees the previous commit present, but that is not here, in your local repo.
To do this, we must force the push with
git push <remote> <branch> --force
or
git push <remote> <branch> -f
Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don’t have in your local branch, you will lose those commits.