How do you rename a Git commit pushed to GitHub?

I stupidly pushed a commit to GitHub with a very messed up commit name. How do I change this?

Does git commit --amend still work for already pushed commit?


Solution 1:

git commit --amend

which will bring up your editor, or

git commit --amend -m "Your new message here"

which will allow you to specify the new message on the command line. Also possible, but more useful if you have other commits to reword

git rebase -i HEAD^
# then replace 'pick' with 'r' or 'reword' and save, editor should pop up again to edit the msg

Because this commit has a new SHA1 due to the change of the contents, you will need to force push the new reference. The force is needed because it tells git to forget about the previous commit. It's a safety measure.

git push origin your-branch-name -f