15

I did a Git commit and push, but wrote the totally wrong thing in the comment.

How do I change the comment? I have already pushed the commit to the remote.

random
  • 9,774
  • 10
  • 66
  • 83
emilan
  • 12,825
  • 11
  • 32
  • 37

3 Answers3

23

git commit --amend will allow you to edit the commit message.

If you already pushed that commit, you need to run git push --force. Only do that if you are sure nobody pulled it yet!

If people pulled the commit from your repo, simply leave the message as it is.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • I did git commit --amend and modified the message, but after that i don't know what to do. Here is all available commands ^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell – emilan Apr 14 '12 at 13:04
  • 2
    So you don't know how to use your default editor?! It looks like `nano`, so simply press `CTRL+X`, followed by `Y`, followed by `RETURN` – ThiefMaster Apr 14 '12 at 13:08
  • sorry it doesn't help me, because I want to change commit message after pushing. git commit --amend doesn't work in this situation. – emilan Apr 14 '12 at 13:19
  • 2
    @emilan If you want to change it, after you pushed, you will need to do as ThiefMaster says and push again with the --force option. – Andy Apr 14 '12 at 14:30
0

If you wrote the wrong thing and the commit has not yet been pushed, you can do the following to change the commit message:

$ git commit --amend

This will open your default text editor, where you can edit the message. On the other hand, you can do this all in one command:

$ git commit --amend -m 'xxxxxxx'

If you have already pushed the message, you can amend the commit and force push, but this is not recommended.

To force push : git push --force

Amitesh Bharti
  • 14,264
  • 6
  • 62
  • 62
0

It is generally not suggested to use --force during git push as it has potential to reset the remote branch to your local branch changes, so the people presently working on the remote branch or sub-branch from the remote branch will be working on out of sync files, as the parent branch is hard reset.

So there is a cleaner way to handle this.

For the latest commit: In your current branch:

  1. git commit --amend -m "New Commit"
  2. git pull (This will merge based on the ort strategy)
  3. git push origin <current_branch>
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88