Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accidentally pushed commit: change git commit message

In my local repo I have one commit with an incorrect commit message.

I've already published the incorrect commit message with git push.

Now the remote repo (which is GitHub-hosted) has the incorrect commit message, too.

I've already tried git commit --amend, but found that it will not work for me in this situation because I've made additional commits since the incorrect one.

How would you fix this situation?

like image 427
jonny Avatar asked Feb 17 '11 17:02

jonny


People also ask

How do I undo a commit message?

On the command line, navigate to the repository that contains the commit you want to amend. Type git commit --amend and press Enter. In your text editor, edit the commit message, and save the commit.

Can you git amend a pushed commit?

Git provides the option to rewrite the most recent commit message. This command will open the editor with the latest commit message. You can then change the commit message and push it. The amend command will open an editor.


2 Answers

Easiest solution (but please read this whole answer before doing this):

  1. git rebase -i <hash-of-commit-preceding-the-incorrect-one>
  2. In the editor that opens, change pick to reword on the line for the incorrect commit.
  3. Save the file and close the editor.
  4. The editor will open again with the incorrect commit message. Fix it.
  5. Save the file and close the editor.
  6. git push --force to update GitHub.

This will mean you will be publishing a modified version of a previously published repository. If anyone pulled or fetched from your repo between when you made the mistake with the incorrect commit message, and when you fixed it, then they will experience some difficulties later. So be sure you can accept this consequence before trying this.

like image 120
Dan Moulding Avatar answered Oct 11 '22 23:10

Dan Moulding


Rather than go the whole rebase route for one commit:

git reset --soft head~ git commit -m "The message you wanted to use" git push -f 

You can see the options in the git-reset manpage.

For a project that only you are working on, the changed history shouldn't be a problem.

like image 41
Abizern Avatar answered Oct 11 '22 22:10

Abizern