从远程 Git 存储库中删除或移除所有历史记录、提交和分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18112966/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:52:59  来源:igfitidea点击:

Delete or remove all history, commits, and branches from a remote Git repo?

git

提问by Stabledog

I've read and tried lots of Git command recommendations and discussion, going on over several days now. It appears that there really is no simple, comprehensive way to make a remote Git repo completely empty -- no branches, no refs, no objects, no files, no nothing.

我已经阅读并尝试了很多 Git 命令建议和讨论,现在已经持续了好几天。似乎真的没有简单、全面的方法可以使远程 Git 存储库完全清空——没有分支、没有引用、没有对象、没有文件、什么也没有

Yes, I recognize that one could delete and recreate the repo-- if one had that kind of permissions on the origin (which I don't), but that is not the point. How is it done? What combination of Git commands will actually do this, leaving the repo in a virgin stateready to receive whatever we wish to push into it, and with essentially no size (or the minimal size of a virgin repo)?

是的,我承认可以删除并重新创建存储库——如果有人对源拥有那种权限(我没有),但这不是重点。它是如何完成的?什么样的 Git 命令组合实际上会做到这一点,让 repo 处于原始状态,准备好接收我们希望推送到它的任何东西,并且基本上没有大小(或原始 repo 的最小大小)

Please don't tell me this shouldn't be done, or that we have to inform all users, etc. I know all that. I just want to start completely fresh.

不要告诉我不应该这样做,或者我们必须通知所有用户等。我知道这一切。 我只想重新开始

回答by

You might want to try pushing an empty local repo with the --mirrorflag(emphasis mine):

您可能想尝试使用--mirror标志推送一个空的本地存储库(重点是我的):

--mirror

Instead of naming each ref to push, specifies that all refs under refs/ (which includes but is not limited to refs/heads/, refs/remotes/, and refs/tags/) be mirrored to the remote repository. Newly created local refs will be pushed to the remote end, locally updated refs will be force updated on the remote end, and deleted refs will be removed from the remote end. This is the default if the configuration option remote.<remote>.mirroris set.

--mirror

不是将每个引用命名为推送,而是指定将 refs/ 下的所有引用(包括但不限于refs/heads/refs/remotes/、 和refs/tags/)镜像到远程存储库。新创建的本地 refs 将被推送到远程端,本地更新的 refs 将在远程端强制更新,删除的 refs 将从远程端删除。如果remote.<remote>.mirror设置了配置选项,则这是默认设置。

If your repo is on GitHub, you'll get this error if masteris set to the default branch when trying to push:

如果您的存储库在 GitHub 上,master并且在尝试推送时设置为默认分支,您将收到此错误:

$ mkdir practice; cd practice;
$ git init; git remote add origin [email protected]:user/practice.git;

$ git push origin --mirror
remote: error: refusing to delete the current branch: refs/heads/master
To [email protected]:user/practice.git
 ! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to '[email protected]:user/practice.git'

I got around this by making an initial commit, then pushing.

我通过进行初始提交,然后推送来解决这个问题。

Obligatory Warning: this will, of course, completely wipe out all of your history and commits in your remote repo—all references, all branches, all tags, etc. Make sure this is actually what you want to do. Of course, you can always make a backup clone of your remote repo before doing this, in case you want to keep it around for whatever reason.

强制性警告:当然,这将完全清除您远程存储库中的所有历史记录和提交——所有引用、所有分支、所有标签等。确保这确实是您想要做的。当然,您始终可以在执行此操作之前制作远程存储库的备份克隆,以防您出于任何原因想要保留它。

Also note that none of the commits will actually be deleted right away. They'll just become danglingcommits, meaning that they're not reachable from a branch. Eventually they'll get garbage collected by Git repos, but if you have access to your remote repo, you can manually start the garbage collection with git gc.

另请注意,实际上不会立即删除任何提交。它们只会成为悬空提交,这意味着无法从分支访问它们。最终它们会被 Git 存储库收集垃圾,但是如果您可以访问远程存储库,则可以使用git gc.

回答by iveqy

You can't do this. The best you can do is to remove all refs and hope that the server runs git gc and has settings for prune objects that doesn't have any refs. This depends on the server configuration.

你不能这样做。您能做的最好的事情是删除所有引用,并希望服务器运行 git gc 并具有用于修剪没有任何引用的对象的设置。这取决于服务器配置。

Usually it takes 14 days before objects are removed by git gc. However those object won't be cloned if you try to clone the repository.

通常需要 14 天才能通过 git gc 删除对象。但是,如果您尝试克隆存储库,则不会克隆这些对象。

You've already got a good answer of how to do a "hack" to remove all refs. It works and your repo will appear to you as it is "fresh". However it isn't.

您已经得到了关于如何进行“hack”以删除所有引用的很好的答案。它可以工作,并且您的回购在您看来是“新鲜的”。然而事实并非如此。