Git: edit previous commits’ messages only

git

For lazy reasons I pushed a bunch of commits with default messages and now it has become cumbersome, as I don't really know what I've changed in each commit.

How do I edit just the messages of previous commits and (if possible) keep the commit tree?

Best Answer

To edit the commit messages of a series of commits, I run

git rebase -i firstsha

where firstsha is an identifier for the parent commit of the first commit I want to edit. (You can use any valid reference here, so git rebase -i HEAD~4 will show the last four commits.)

In the editor that opens, change all the “pick” entries to “reword” on commits you wish to modify, then close the editor; you will then be asked to enter commit messages for all the commits you chose.

Note that this will change the commit tree, because the hashes of the commits will change. You will have to force-push your new tree, or push it to a new branch. Take care when rebasing merges; you’ll need to use -r (--rebase-merges), and read the “Rebasing merges” section of the git rebase manpage.

To quickly edit only the last commit, run

git commit --amend

(but beware of anything already staged for commit).

Related Question