Edit the root commit in Git?

前端 未结 5 1202
予麋鹿
予麋鹿 2020-11-22 16:33

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

git commit --amend                    # for the most recent co         


        
相关标签:
5条回答
  • 2020-11-22 17:13

    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^

    0 讨论(0)
  • 2020-11-22 17:21

    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
    
    0 讨论(0)
  • 2020-11-22 17:25

    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.

    0 讨论(0)
  • 2020-11-22 17:30

    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
    
    0 讨论(0)
  • 2020-11-22 17:39

    As of Git version 1.7.12, you may now use

    git rebase -i --root
    

    Documentation

    0 讨论(0)
提交回复
热议问题