Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. Like, for instance, another git repository with local branches that I haven't pushed yet.

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. 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? Ideally, it should also check that the repo I'm trying to delete has some up-to-date remote, before actually deleting it.


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?

like image 566
leftaroundabout Avatar asked Mar 09 '23 11:03

leftaroundabout


2 Answers

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.

like image 181
j6t Avatar answered Mar 12 '23 00:03

j6t


You may create a little bash function in your .bashrc:

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:

rmgit /path/to/clone
like image 36
hek2mgl Avatar answered Mar 12 '23 01:03

hek2mgl