Posted on April 26, 2013 by
Spread the word...

how-to-edit-incorrect-commit-message-in-git

So, you have pushed an incorrect commit message?

Let’s admit it we have all pushed a bad or incorrect commit message, it happens to the best of us.

Try the following:

git commit --amend -m "New commit message"

That command should be used whenever you want to amend the tip of the current branch. The commit you create replaces the current tip — if it was a merge, it will have the parents of the current tip as parents — so the current top commit is discarded. It is a rough equivalent for the following, but can be used to amend a merge commit.

$ git reset --soft HEAD^
$ ... do something else to come up with the right tree ...
$ git commit -c ORIG_HEAD

Can I use this to edit the commit message on Github?

Nope. The command highlighted above does not work on Github website as this is not directly possible. The hash for every Git commit is also calculated based on the commit message. When you change the commit message, you change the commit hash. If you want to push that commit, you have to force that push (git push -f). But if already someone pulled your old commit and started a work based on that commit, he would have to rebase his work onto your new commit.

Steps to edit a git commit message and also have it show up to Githb:

1. To edit your last commit message use:

git commit -amend

2. Edit your message and push to Github:

git push

At this stage you might get an error message like:

$ git push
To git@github.com:your/repo.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:your/repo.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

If you get that message you should use –force:

git push --force

 

Leave a Comment

Your email address will not be made public or shared. Inappropriate and irrelevant comments will be removed.