如何在 git 中硬删除孤立提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7907372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hard delete an orphan commit in git?
提问by FMaz008
I have local commit that are not on any branch that I would like to delete. I don't want to rebase them, I really want to delete them, and loose all the content related to these commit.
我有本地提交不在我想删除的任何分支上。我不想改变它们的基础,我真的想删除它们,并释放与这些提交相关的所有内容。
Is their a command to do so ?
他们有这样做的命令吗?
So far I've tried interactive rebase as many suggested, but it just move commit around, it doesn't delete them. I've also tried to use reflog delete, but I can't figure out how to pass a specific commit Id to the command.
到目前为止,我已经尝试了许多建议的交互式 rebase,但它只是移动提交,它不会删除它们。我也尝试使用 reflog delete,但我不知道如何将特定的提交 ID 传递给命令。
Here's the working tree:
这是工作树:
o [master] Commit #6
|
o Commit #5
|
| o Commit #4
|/
o Commit #3
|
o Commit #2
|
o Commit #1
I want to physically delete the commit #4.
我想物理删除提交 #4。
回答by Andy
If the commit is not referenced by anything, it will be removed with git's garbage collection, in time.
如果提交没有被任何东西引用,它将被 git 的垃圾收集及时删除。
If you want to force it before hand, use git gc --prune=now --aggressive
如果您想事先强制执行,请使用 git gc --prune=now --aggressive
回答by Dan Lynch
To remove a commit, you just need to make sure that it's no longer part of any branch. That is, for each branch in your repository, make sure the bad commit is not part of the child-to-parent chain of commits defining that branch. If you do find such a branch, either delete it, or change its history so that it no longer includes the bad commit.
要删除提交,您只需要确保它不再是任何分支的一部分。也就是说,对于存储库中的每个分支,请确保错误提交不是定义该分支的子级到父级提交链的一部分。如果确实找到了这样的分支,请将其删除,或更改其历史记录,使其不再包含错误提交。
Once that's true, the bad commit will be garbage-collected eventually.
一旦这是真的,错误的提交最终将被垃圾收集。
回答by TamaMcGlinn
To fully delete a commit, you need to delete all references to it, and then force an aggressive garbage-collection.
要完全删除提交,您需要删除对它的所有引用,然后强制进行积极的垃圾收集。
If you still have branches pointing to the commit, delete those first.
如果您仍然有指向提交的分支,请先删除它们。
Next you will need to flush the commit from the reflog, which keeps references to every commit you have visited in the past 90 days by default (replace [commit-hash] with the first 8 characters of the commit hash):
接下来,您需要从 reflog 中刷新提交,默认情况下,它会保留对您在过去 90 天内访问过的每个提交的引用(将 [commit-hash] 替换为提交哈希的前 8 个字符):
git reflog | grep [commit-hash] | cut -d' ' -f2 | cut -d':' -f1 | tac | xargs git reflog delete
(I used tac
to reverse the order of deletions because if the commit shows up multiple times in the reflog, deleting an entry shifts all the entries following it)
(我曾经tac
颠倒删除的顺序,因为如果提交在 reflog 中出现多次,删除一个条目会移动它后面的所有条目)
Finally, force the garbage collection:
最后,强制垃圾回收:
git gc --prune=now --aggressive
After this, git show [commit-hash]
will no longer show the commit, so you will know it is really deleted.
在此之后,git show [commit-hash]
将不再显示提交,因此您将知道它真的被删除了。