412

There's ways to change the message from later commits:

git commit --amend                    # for the most recent commit
git rebase --interactive master~2     # but requires *parent*

How can you change the commit message of the very first commit (which has no parent)?

3

5 Answers 5

703

As of Git version 1.7.12, you may now use

git rebase -i --root

Documentation

6
  • 2
    is it possible to rebase the root of all branches using this command? Seems like this will detach the current branch onto the new root and all the other branches will stay on the old root
    – woojoo666
    Commented Mar 22, 2015 at 7:29
  • @woojoo666 you will have to rebase branches onto new root then. as usual.
    – berkus
    Commented Apr 24, 2015 at 12:13
  • @Atcold it doesn't work if there is no upstream root
    – Kai
    Commented May 20, 2016 at 5:31
  • 2
    FWIW: I would also like to see this as the accepted answer, especially as it matches my all-time favourite git command for cleaning up the history of new projects in the early stages of development, namely: git rebase --interactive --autosquash --autostash --root Commented Jun 13, 2016 at 8:56
  • 2
    @Leo what does your comment mean? I can't see the link between the first part and the second - what does taking a while have to do with it?
    – boycy
    Commented Nov 29, 2017 at 12:29
323

Assuming that you have a clean working tree, you can do the following.

# checkout the root commit
git checkout <sha1-of-root>

# amend the commit
git commit --amend

# rebase all the other commits in master onto the amended root
git rebase --onto HEAD HEAD master
21
  • 28
    I believe this should be git rebase --onto HEAD <sha1-of-root> master.
    – Andrew
    Commented May 9, 2012 at 20:58
  • 5
    Right, but you want the original root commit for the <upstream> of git rebase. git rebase applies commits in <branch> (master) that are not in <upstream>; HEAD is not in master, so your version tries to apply all of master.
    – Andrew
    Commented May 16, 2012 at 18:41
  • 8
    Yes, make sure it's git rebase --onto HEAD <sha1-of-root> master, where <sha1-of-root> is the same used in git checkout <sha1-of-root>. Otherwise, you'll have 2 first commit's.
    – Andy
    Commented Jun 18, 2012 at 19:01
  • 3
    @Cupcake: Did you test the old version of the command? It should work fine. The amend is changing the commit message only so the old and new root commits introduce exactly the same changes so the old root commit is skipped automatically. The second HEAD ensures that all commits are considered and that we can use the two parameter version of rebase to move back onto master. Please note that this answer predates the existence of the --root option to rebase.
    – CB Bailey
    Commented Jul 15, 2013 at 7:06
  • 11
    ecdpalma's answer below is much easier and simpler and has more votes, scroll down people!
    – Flimm
    Commented Oct 2, 2015 at 13:37
80

To expand on ecdpalma's answer, you can now use the --root option to tell rebase that you want to rewrite the root/first commit:

git rebase --interactive --root

Then the root commit will show up in the rebase TODO list, and you can select to edit or reword it:

reword <root commit sha> <original message>
pick <other commit sha> <message>
...

This is the explanation of --root from the Git rebase docs (emphasis mine):

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.

1
  • only changes the commit message of the root inside 1 branch, how to do it on all 25 branches
    – PirateApp
    Commented Jul 13, 2022 at 3:56
17

Just to provide an alternative to the higher rated answers:

If you are creating a repo, and know upfront that you'll be rebasing on top of its "first" real commit in the future, you can avoid this problem altogether by making an explicit empty commit at the beginning:

git commit --allow-empty -m "Initial commit"

and only then start doing "real" commits. Then you can easily rebase on top of that commit the standard way, for example git rebase -i HEAD^

3
  • 7
    Doesn't this mean that, in order for this to work, you need to have the foresight (or be psychic) to make an empty commit right at the very beginning of your project? This seems to be extremely situational, to me, and generally not practical. What do you think? What happens if I've already made 100 commits, and I suddenly need to edit the root commit. Will this still work, in that case, if I didn't make that empty commit at the start?
    – user456814
    Commented Jul 20, 2014 at 6:58
  • 5
    Editing the message of the root commit is probably not something you would do after having 100s of them. I sometimes happen to just want to have a git repo, doing some trashy commits, knowing that once I reach some usable state, I'd squash them into one for instance, and reword the message. Anyway, now I changed my mind and I think the absolutely most useful thing for the first commit would be putting .gitattributes file instead of doing an empty commit.
    – jakub.g
    Commented Jul 20, 2014 at 23:51
  • Reply to the first commenter: You don’t have to have the foresight to make an empty commit every time. You could (1) create a repository with this empty commit (2) place it in some “git repository templates” directory (3) copy it over whenever you need a new repository. Commented May 1, 2023 at 8:23
4

You could use git filter-branch:

cd test
git init

touch initial
git add -A
git commit -m "Initial commit"

touch a
git add -A
git commit -m "a"

touch b
git add -A
git commit -m "b"

git log

-->
8e6b49e... b
945e92a... a
72fc158... Initial commit

git filter-branch --msg-filter \
"sed \"s|^Initial commit|New initial commit|g\"" -- --all

git log
-->
c5988ea... b
e0331fd... a
51995f1... New initial commit
1
  • I'm using filter-branch change the author / committer, and the -- --all option indeed is the key in this case to be able to also handle the root commit.
    – sschuberth
    Commented Feb 6, 2014 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.