How to change the commit message of a commit in the history (not HEAD)?

A commit is not the HEAD now. and git commit --amend will not work for it.

How to change its commit message?

You can use git rebase together with git commit --amend as follows. Assume your commit to be edited is 2b8ed.

$ git rebase -i 2b8ed^

Change the pick to edit in the line contains 2b8ed.

Now, you have moved back to the commit before 2b8ed and you can do the commit message changing as before by:

$ git commit --amend

Then run:

$ git rebase --continue

This command applies the following commits automatically to the original HEAD.

Be very careful when doing this if the commit has already been pushed to a git server and is shared with others. The others may need to rebase to get the changed history.

Note: read more about rewritting history in Pro Git.

Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *