4

I fixed some code and pushed it with an incorrect commit message. When I tried to fix the commit message, by accident I reverted the commit. The situation is now:

  • latest commit = correct commit message, but changes of previous commit undone
  • previous commit = correct commit content, but wrong commit message

All of this has already been pushed to the server.

I would like to get rid of the latest commit, change the commit message of the previous commit and push that back to the server.

I'm happy with solutions for Atlassian Source Tree but also command line is okay.

Krumelur
  • 32,180
  • 27
  • 124
  • 263

2 Answers2

10

If it's the last commit, it's easy:

git commit --amend

This brings up the editor with the last commit message and lets you edit the message. (You can use -m if you want to wipe out the old message and use a new one.)

And then when you push, do this:

git push --force <repository> <branch>

Reference: Changing git commit message after push (given that no one pulled from remote)

Community
  • 1
  • 1
rafaels88
  • 839
  • 1
  • 6
  • 19
0

You shouldn't amend a commit which has already been pushed publicly because your teammates probably have already pulled it and git will merge your new commit with the old commit when they pull and bring it back when they push (see this chapter in progit to understand details).

I suggest, that you leave it as it is (maybe just add a new commit with extended message so people get what you wanted in the original commit message after all).

In case you really need this to be wiped from history you'd need all your teammates who pulled your commit to rebase their new commits on your new version of the commit in question.

Isantipov
  • 19,491
  • 2
  • 26
  • 41