Git remove root commit

后端 未结 2 1909
心在旅途
心在旅途 2020-12-11 15:49

I can\'t do a lot of things with git and I want to remove a commit from my repo, because I uploaded wrong stuff.

I used git revert bu

相关标签:
2条回答
  • 2020-12-11 16:33

    For removing a root commit, you simply have to remove all branches (and tags) from which it is reachable.

    This can be done with git branch -D branch-name. (You will have to first check out another branch which does not refer to this root commit, since you can't remove the current branch, I think.)

    If you want to retain other commits on this branch and only remove the root, git filter-branch is better, see the answer from Greg.

    0 讨论(0)
  • 2020-12-11 16:35

    You can do this using git filter-branch. First, identify the commit ID at the root that you want to remove. I'll represent that with <the_commit>. Then, run git filter-branch using --parent-filter and a sed command that snips off that parent:

    git filter-branch --parent-filter "sed 's/-p <the_commit>//'" HEAD
    

    Here's a transcript of an example I just tried:

    $ git log
    commit 7e1ba37b51fc2cc6289cf66367c9aedc74c664a8
    Author: Greg Hewgill <greg@example.com>
    Date:   Fri May 27 20:54:27 2011 +1200
    
        three
    
    commit a8a410d2361824cbd518a48225e9402a691be93f
    Author: Greg Hewgill <greg@example.com>
    Date:   Fri May 27 20:54:17 2011 +1200
    
        two
    
    commit 3171d512d98f6bc5f3c2469312930c0d32d3aa07
    Author: Greg Hewgill <greg@example.com>
    Date:   Fri May 27 20:54:00 2011 +1200
    
        one
    $ git filter-branch --parent-filter "sed 's/-p 3171d512d98f6bc5f3c2469312930c0d32d3aa07//'" HEAD
    Rewrite 7e1ba37b51fc2cc6289cf66367c9aedc74c664a8 (3/3)
    Ref 'refs/heads/master' was rewritten
    $ git log
    commit 489ec1ee20e0dd20cd835ceebf157f628cd75a44
    Author: Greg Hewgill <greg@example.com>
    Date:   Fri May 27 20:54:27 2011 +1200
    
        three
    
    commit a6f5ee410c9ea4fca6fbff265149b7fc555241eb
    Author: Greg Hewgill <greg@example.com>
    Date:   Fri May 27 20:54:17 2011 +1200
    
        two
    $ 
    
    0 讨论(0)
提交回复
热议问题