2011-05-27 142 views
11

我不能用git做很多事情,我想从我的repo中删除提交,因为我上传了错误的东西。Git删除根提交

我用git revert <the_commit>但由于提交是根,我不能删除它。 致命:无法恢复根提交

在这种情况下该做什么?

请不要给我这里的其他主题的链接,我阅读他们,我不明白该怎么做,我需要一些关于删除一些提交的基本示例。

回答

4

用于去除根犯,你只需要删除它可以到达的所有分支(和标签)。

这可以用git branch -D branch-name完成。 (你必须首先检查另一个没有提到这个根提交的分支,因为你不能删除当前的分支,我想。)

如果你想保留这个分支上的其他提交并且只删除根,git filter-branch更好,请参阅Greg的答案。

18

您可以使用git filter-branch来做到这一点。首先,确定要删除的根的提交ID。我会用<the_commit>来表示。

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

下面是一个例子的抄本我只是想:然后,用--parent-filter和剪断sed命令父运行git filter-branch

$ git log 
commit 7e1ba37b51fc2cc6289cf66367c9aedc74c664a8 
Author: Greg Hewgill <[email protected]> 
Date: Fri May 27 20:54:27 2011 +1200 

    three 

commit a8a410d2361824cbd518a48225e9402a691be93f 
Author: Greg Hewgill <[email protected]> 
Date: Fri May 27 20:54:17 2011 +1200 

    two 

commit 3171d512d98f6bc5f3c2469312930c0d32d3aa07 
Author: Greg Hewgill <[email protected]> 
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 <[email protected]> 
Date: Fri May 27 20:54:27 2011 +1200 

    three 

commit a6f5ee410c9ea4fca6fbff265149b7fc555241eb 
Author: Greg Hewgill <[email protected]> 
Date: Fri May 27 20:54:17 2011 +1200 

    two 
$