66

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?

Nhan
  • 3,595
  • 6
  • 30
  • 38
adit
  • 32,574
  • 72
  • 229
  • 373
  • Use `git commit --amend` followed by `git push --force`. – vanza Jul 22 '12 at 19:52
  • 2
    I would not recommend `git push --force`. You want to be explicit and only force that what you need to be forced. For example: `git push --force origin specific-branch`. You can also use the short form of force `-f`. Short forms can be joined together as one option as well. Eg. `git push -uf origin mybranch` – Adam Dymitruk Jul 22 '12 at 21:24
  • Related question - [How do I git rebase the first commit](https://stackoverflow.com/a/23000315/6908282) & related [arcticle](https://medium.com/@_oleksii_/how-to-change-a-commit-message-in-git-after-push-32d1fbd6fc3b) – Gangula Nov 11 '22 at 11:00

2 Answers2

135
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
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
0

To make changes in already pushed commit, please do that

git reset --soft HEAD~1
git add .
git commit -m "custom message"
git push -u -f origin master
Guru
  • 922
  • 9
  • 12