Change first commit of project with Git?

This question already has an answer here:

  • Edit the root commit in Git? 5 answers

  • As mentioned by ecdpalma below, git 1.7.12+ (August 2012) has enhanced the option --root for git rebase :

    " git rebase [-i] --root $tip " can now be used to rewrite all the history leading to " $tip " down to the root commit.

    That new behavior was initially discussed here:

    I personally think " git rebase -i --root " should be made to just work without requiring " --onto " and let you "edit" even the first one in the history.
    It is understandable that nobody bothered, as people are a lot less often rewriting near the very beginning of the history than otherwise.

    The patch followed.


    (original answer, February 2010)

    As mentioned in the Git FAQ (and this SO question), the idea is:

  • Create new temporary branch
  • Rewind it to the commit you want to change using git reset --hard
  • Change that commit (it would be top of current HEAD, and you can modify the content of any file)
  • Rebase branch on top of changed commit, using:

    git rebase --onto <tmp branch> <commit after changed> <branch>`
    
  • The trick is to be sure the information you want to remove is not reintroduced by a later commit somewhere else in your file. If you suspect that, then you have to use filter-branch --tree-filter to make sure the content of that file does not contain in any commit the sensible information.

    In both cases, you end up rewriting the SHA1 of every commit, so be careful if you have already published the branch you are modifying the contents of. You probably shouldn't do it unless your project isn't yet public and other people haven't based work off the commits you're about to rewrite.


    如1.7.12发行说明中所述,您可以使用

    $ git rebase -i --root
    

    git rebase -i allows you to conveniently edit any previous commits, except for the root commit . The following commands show you how to do this manually.

    # tag the old root, "git rev-list ..." will return the hash of first commit
    git tag root `git rev-list HEAD | tail -1`
    
    # switch to a new branch pointing at the first commit
    git checkout -b new-root root
    
    # make any edits and then commit them with:
    git commit --amend
    
    # check out the previous branch (i.e. master)
    git checkout @{-1}
    
    # replace old root with amended version
    git rebase --onto new-root root
    
    # you might encounter merge conflicts, fix any conflicts and continue with:
    # git rebase --continue
    
    # delete the branch "new-root"
    git branch -d new-root
    
    # delete the tag "root"
    git tag -d root
    
    链接地址: http://www.djcxy.com/p/23436.html

    上一篇: 编辑Git中的根提交?

    下一篇: 用Git改变项目的第一次提交?