skip to content
Alvin Lucillo

Modify a non-root commit

/ 3 min read

Yesterday, we saw how we can edit a root commit and add files to it. We can see that it performed a rebase of the commit and all commits after it. In today’s entry, I will modify the description of a non-root commit.

Below we can see that there are 3 commits. We are interested in modifying the description of commit c168fa314073bf654dea0785500b14596fd9be50

$ git log --oneline
c9a147c add version 3
c168fa3 modify notes
1d96512 add notes and metadata

$ git log --stat
commit c9a147c4f91fe1ae64ce00b7ed3329bcbd49fb8b
Author: <redacted>
Date: <redacted>

    add version 3

 notes | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit c168fa314073bf654dea0785500b14596fd9be50
Author: <redacted>
Date: <redacted>

    modify notes

 notes | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit 1d96512da874be9b1827c2dccb2d336255667cdf
Author: <redacted>
Date: <redacted>

    add notes and metadata

 extra    | 1 +
 metadata | 1 +
 notes    | 1 +
 3 files changed, 3 insertions(+)

Since the commit is not a latest commit, we can’t just amend it; we need to perform a rebase. In the command below, the rebase command provides a caret ^ at the end of the commit we want to modify. This means we want to start with the parent of that commit. Note that rebase starts the changes in the commits following the specified commit. Alternatively, we specify the commit of the parent if we have it handy (i.e., git rebase -i 1d96512da874be9b1827c2dccb2d336255667cdf)

$ git rebase -i c168fa314073bf654dea0785500b14596fd9be50^

pick c168fa3 modify notes
pick c9a147c add version 3

Like with yesterday’s demo, we need to modify pick beside the commit we want to modify. Since this demo will just modify the description, we have to use reword

reword c168fa3 modify notes
pick c9a147c add version 3

When editor for the text above exits, it shows the current description of the commit.

modify notes

I modified the description.

modify notes and add version

When the editor for the text above exits, it shows successful rebase message.

[detached HEAD c3f49dc] modify notes and add version
 Date: <redacted>
 1 file changed, 1 insertion(+), 1 deletion(-)
Successfully rebased and updated refs/heads/main.

Notice that parent commit hash didn’t change (1d96512da874be9b1827c2dccb2d336255667cdf); it’s the commits after it that have new commit hashes.

$ git log --oneline
bfdb0a5 add version 3
c3f49dc modify notes and add version
1d96512 add notes and metadata

$ git log --stat
commit bfdb0a5890c90f514d272631a25b3c66d7bba4e1
Author: <redacted>
Date: <redacted>

    add version 3

 notes | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit c3f49dcc557ca878e073eb9089c8cb22339b00b9
Author: <redacted>
Date: <redacted>

    modify notes and add version

 notes | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit 1d96512da874be9b1827c2dccb2d336255667cdf
Author: <redacted>
Date: <redacted>

    add notes and metadata

 extra    | 1 +
 metadata | 1 +
 notes    | 1 +
 3 files changed, 3 insertions(+)