3

I have created a commit with a commit message that I want to modify. I have not published the commit, yet, so I can rewrite history safely. I can find using git log, so I know its sha1 hash. How can I quickly edit the commit?

Bengt
  • 14,011
  • 7
  • 48
  • 66

2 Answers2

6

You can checkout the commit in question, amend its message and rebase back to your branch manually:

$ git checkout FIRST_COMMIT_SHA
$ git commit --amend
$ git rebase HEAD THE_BRANCH_YOU_CAME_FROM

This git alias will automate this process:

reword = "!f() { branch=`git symbolic-ref --short HEAD`; git checkout $1; git commit --amend; git checkout $branch; }; f"

To add it to your ~/.gitconfig:

$ git config alias.reword "!f() { branch=`git symbolic-ref --short HEAD`; git checkout $1; git commit --amend; git checkout $branch; }; f"

Then use like this:

$ git reword SHA1_OF_THE_COMMIT_TO_BE_REWORDED

Credits:

Community
  • 1
  • 1
Bengt
  • 14,011
  • 7
  • 48
  • 66
1

Alternatively, amending the initial commit message can be accomplished by using the rebase command and supplying the --root flag. In addition, you would need to specify interactive mode and use edit for the first commit, like so:

git rebase -i -root

// Specify 'edit' for the first commit.

// Amend first commit message here.
git commit --amend

See here for further details on the --root flag.

Also, provided that the commit with the message you want to modify is in the branch you are working on, you could easily solve that with interactive rebase too. Simply locate the corresponding short SHA-1 and specify edit to allow modification of its commit message.

miqh
  • 3,624
  • 2
  • 28
  • 38
  • This only works, if the commit to be reworded is the root commit. Since OP (me) did not specify that may be the case, or not. – Bengt Dec 09 '13 at 11:25