Search code examples
gitrebase

Modifying the earliest commit in the list with git rebase


I have a list of commits that I'd like to change the message for. There are 14 commits in total. I've been using git rebase to change some of these messages when needed. This has mostly proved relatively simple. But it seems that things don't quite work the same way for the earliest commit in the list (number 14).

Specifying the location of HEAD~14 returns fatal: invalid upstream error. Calling on rebase by directly specifying the SHA of commit 14 returns a list with only the first 13 commits in the editor.

While I have only a very partial understanding of how rebase works conceptually / abstractly, my intuition tells me that this is likely because in any list of 'n' items, at least one of these must be the common ancestor of two branches, which means rebase can't be used on commit 'n' to make changes as it can with others.

I suppose I have two questions, really:

  1. Is my understanding outlined above along the right track?

  2. Practically, what can I do if I would like to change the commit message for the earliest commit in my list?

For context, I'm rather new to using Git. Trying to wrap my head around some of the main concepts and understanding what they mean, abstractly, has been a struggle so far and I think this is probably why I'm often perplexed by results.

It might be worth adding, as a final bit of context, that I'm using Git in an entirely private capacity for an amateur / hobbyist project. My repository is entirely local. So any considerations about how rewriting history might affect collaborators shouldn't apply in this case.


Solution

  • Normally you pass rebase the parent of the earliest commit you want to rebase. That doesn't work if you want to rebase the first commit as it has no parent. There's a special case --root option to handle that scenario:

    git rebase -i --root
    

    From the documentation:

    --root
    

    Rebase all commits reachable from <branch>, instead of limiting them with an <upstream>. This allows you to rebase the root commit(s) on a branch.