Git 删除根提交

标签 git commit root

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

我用了git revert <the_commit>但由于提交是根目录,我无法删除它。 致命:无法还原根提交

遇到这种情况怎么办?

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

最佳答案

您可以使用 git filter-branch 执行此操作.首先,确定要删除的根目录下的提交 ID。我将用 <the_commit> 表示.然后,运行 git filter-branch使用 --parent-filter和一个 sed切断父级的命令:

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

这是我刚试过的一个例子的文字记录:

$ 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
$ 

关于Git 删除根提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6149520/

相关文章:

linux - sshd_config 上允许用户,忘记设置 root

swift - 在 swift 应用程序中以 root 身份运行命令

GIT: checkout 到特定文件夹

git - Control-C 在 Windows 7 的 git bash 中杀死 Ipython

Git 从全局设置中获取用户名而不是本地设置

git - 从整个 git 历史记录中删除文件

linqpad 中的根类

git - 用孤儿分支重写 master 但保留历史记录

xcode - 将字体添加到react-native项目

git - 从不同的存储库中 pull 提交