Edit the root commit in Git?

50,254

Solution 1

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

Solution 2

As of Git version 1.7.12, you may now use

git rebase -i --root

Documentation

Solution 3

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^

Solution 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
Share:
50,254
13ren
Author by

13ren

(your about me is currently blank)

Updated on December 27, 2020

Comments

  • 13ren
    13ren over 3 years

    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)?