2011-05-27 139 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 
$