I have a commit that was pushed to the remote and I want to change its message.
I know that I can use git push -f
to force push different commit but first I need to remove old commit and keep changes, so I can commit again.
How can I do this?
As a disclaimer, this answer involves rewriting the history of the branch in question. As such, it should be used with caution on branches which have been publicly shared.
The easiest fix here, assuming that no one else has pushed to the remote counterpart of the branch, would be to simply amend the commit via:
git commit --amend
This should bring up a text editor where you can change the commit message to whatever you want.
As you mentioned, you will now have to force push this branch to the remote via:
git push --force origin yourBranch
If you want to change the latest commit, use
git commit --amend
However, for already pushed commits, you should only do this if you are sure that nobody has checked out the commit that you are overwriting.
well you need to use the following command git commit --amend
which will create the same commit locally but with correct message, then you push it to remote
Using:
git commit --amend
seems to be the obvious solution here, but you didn't mention any branch in your question so that answer may not be correct solution in your case. If the commit you want to rename is on a branch that only you created and used so far and hasn't been merged into another branch, I suggest another option which is:
Still, it really depends whether the commit were on a separate branch or on a major branch (e.g. master).