简体   繁体   English

Git 删除根提交

[英]Git remove root commit

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.我不能用 git 做很多事情,我想从我的仓库中删除一个提交,因为我上传了错误的东西。

I used git revert <the_commit> but since the commit is root and I can't remove it.我使用git revert <the_commit>但由于提交是 root 并且我无法删除它。 fatal: Cannot revert a root commit致命:无法还原根提交

What to do in that case?在这种情况下该怎么办?

Please don't give me links to other topics here, I read them and I don't understand what to do, I need some basic example about removing some commit.请不要在这里给我指向其他主题的链接,我阅读了它们,但我不明白该怎么做,我需要一些关于删除某些提交的基本示例。

You can do this using git filter-branch .您可以使用git filter-branch来执行此操作。 First, identify the commit ID at the root that you want to remove.首先,确定要删除的根目录中的提交 ID。 I'll represent that with <the_commit> .我将用<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命令剪断该父级:

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
$ 

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 .这可以通过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.如果您想保留此分支上的其他提交并且只删除根, git filter-branch更好,请参阅 Greg 的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM