简体   繁体   English

如何在没有rm -rf sledgehammer的情况下删除单个git存储库克隆?

[英]How to delete a single git repository clone, without the rm -rf sledgehammer?

It is not possible to remove a git repo simply with rm -r , because the .git directory contains lots of write-protected files. 简单地使用rm -r删除git repo是不可能的,因为.git目录包含许多写保护文件。 I have permission to delete these as I'm the owner, but it's not feasible to confirm them all one by one. 有权删除这些因为我是所有者,但是逐一确认它们是不可行的。

Now, it is possible to do it all in one command by adding the -f flag to rm , but that means I lose all protection against deleting stuff that's protected for good reason. 现在,可以通过向rm添加-f标志在一个命令中完成所有操作,但这意味着我将失去所有保护,以防止删除受到保护的内容。 Like, for instance, another git repository with local branches that I haven't pushed yet. 比如,例如,另一个带有本地分支的git存储库,我尚未推送。

But it seems the only way, so I've found myself doing rm -rf quite a lot lately, but I always feel uneasy about it. 但这似乎是唯一的方法,所以我发现自己最近做了很多rm -rf ,但我总是对此感到不安。 Is there a better way to get rid of a single repo clone, that will not ask about thousands of individual files but will fail if I try to erase something but a single repo? 是否有更好的方法来摆脱单个repo克隆,这不会询问成千上万的单个文件,但如果我尝试删除一些东西而不是单个回购邮件失败? Ideally, it should also check that the repo I'm trying to delete has some up-to-date remote, before actually deleting it. 理想情况下,在实际删除它之前,它还应该检查我尝试删除的repo是否有一些最新的遥控器。


I don't quite see what's unclear about this question... it is this: 我不太清楚这个问题有什么不清楚......就是这样:

How do I delete a repository with all contents, using commands that would not also nuke any other directory with write-protected files in it? 如何删除包含所有内容的存储库,使用的命令不会破坏其中包含写保护文件的任何其他目录?

First, you collect all garbage objects: 首先,您收集所有垃圾对象:

git gc --prune=all

Then only one pack file remains, which is read-only, and the corresponding pack index, which is also read-only. 然后只剩下一个包文件,它是只读的,以及相应的包索引,它也是只读的。 Now 现在

cd ..
rm -r repo

will ask only twice for confirmation instead of thousands of times. 只会要求两次确认而不是数千次。

This does not protect against removal of checked-out submodules, though. 但是,这并不能防止删除已签出的子模块。

You may create a little bash function in your .bashrc: 您可以在.bashrc中创建一个小bash函数:

function rmgit() {
    # Recursively apply write permissions to files in the .git folder
    chmod -R +w "${1}/.git"
    # Remove the clone
    rm -r "${1}"
}

Start a new shell and you should now being able to: 启动一个新shell,你现在应该能够:

rmgit /path/to/clone

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

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