How to rewrite a comment of a specific commit

Sometimes you need to rewrite a comment of a specific commit. This is how you do it.

The git commit --amend command allows you to rewrite the comment of the last commit (see here). But what if you need to rewrite the comment of a specific commit further back in your commit history?

The commands below rewrite the history, so you must proceed with care if the commits have already been pushed to a remote repository.

Your commit history looks like this:

alina

alina git:( main ) git log --graph --oneline --all

* 3ac38c7 (HEAD -> main) add line D

* da69305 add xxx line C

* 1f8b884 add line B

* af1b7f5 add line A

* f299332 (origin/main, origin/HEAD) Initial commit

alina git:( main )

and you want to change the comment of the commit da69305 (to remove xxx).

To do that, you can use the git rebase [-i | --interactive] <commit>~ command, where <commit> is the hash of the commit you want to change.

In this case, you want to change the commit da69305, so you run git rebase -i da69305~. The ~ is necessary so that the rebase starts from the parent of the commit you want to change.

When you run the git rebase command, your editor will open with a list of commits to rebase:

pick da69305 add xxx line C

pick 3ac38c7 add line D

# Rebase 1f8b884..3ac38c7 onto 1f8b884 (2 commands)

#

# Commands:

# p, pick <commit> = use commit

# r, reword <commit> = use commit, but edit the commit message

# e, edit <commit> = use commit, but stop for amending

# s, squash <commit> = use commit, but meld into previous commit

 ... omitted for brevity ...

In our case, since we want to change the comment of the commit da69305, we replace pick with reword:

reword da69305 add xxx line C

pick 3ac38c7 add line D

 ... omitted for brevity ...

Once you save the file, the rebase will start and your editor will open with the comment of the commit you want to change:

add xxx line C

# Please enter the commit message for your changes. Lines starting

# with '#' will be ignored, and an empty message aborts the commit.

#

You can then edit the comment, save the file, and the rebase will continue.

alina

alina git:( main ) git rebase -i da69305~

[detached HEAD 51a4279] add line C

 Date: Wed Jun 28 07:43:56 2023 -0700

 1 file changed, 1 insertion(+)

Successfully rebased and updated refs/heads/main.

alina git:( main )

Running the git log command will now show that:

  • the message of the commit da69305 (now 51a4279) has been changed
  • all the commit ids after da69305 have also changed

alina

alina git:( main ) git log --graph --oneline --all

* 95998de (HEAD -> main) add line D

* 51a4279 add line C

* 1f8b884 add line B

* af1b7f5 add line A

* f299332 (origin/main, origin/HEAD) Initial commit

alina git:( main )

After the rebase, your commit graph will look like this:

alina

95998de51a42793ac38c7da693051f8b884af1b7f5f299332mainHEAD

The graph of commits has been changed, which is why it is not recommended to do this on commits that have already been pushed to a remote repository.