Can I remove a git repo without resorting to rm -rf ?
Git repos have lots of write protected files in the .git directory, sometimes hundreds, and the default rm my_project_managed_by_git will prompt before deleting each write protected file. So, to actually delete my project I have to do rm -rf my_project_managed_by_git.
Using rm -rf scares me. Is there a reasonable way to delete git repos without it?
cd git-project
rm -rf .git
cd ..
rm -r git-project
With rm -r is for (R)ecursion and -f is for F(force) disabled the prompting. So, use -f on the .git directory which has the files you want to obliterate, and leave it off for the safety prompts.
I’ve shot myself in the foot enough times over the years with rm -rf. Now I use trash-cli. I don’t know what package manager(s) you use, but I install it via Homebrew.
That's a good suggestion for some, but I'm quite comfortable with the command line.
It's not that I'm irrationally scared of rm -rf. I know what that command will do. If I slow down an pay attention it's not as though I'm worried "I hope this doesn't break my system".
What I really mean is I see myself becoming quite comfortable typing rm -rf and running it with little thought, I use it often to delete git repos, and my frequent use and level of comfort with this command doesn't match the level of danger it brings.
Just moving them to /tmp is a nice suggestion that can work on anywhere without special programs or scripts.
If you're making backups of things you care about and not running sudo rm -rf the command isn't really dangerous.
But +1 for having it in /tmp
I have a bash function I call tempd that is basically cd $(mktemp -d) I use it so much for stuff I dont really care to keep.
Additionally you could move the git folder to the trash folder. I think it's usually located at $HOME/.local/share/trash/files/
Moving something to the trash files folder isn't the correct way to trash it, since the Trash specification requires storing some metadata for each trash item.
If you’re that worried, why not run chmod -R u+w .git inside the project dir to “un write-protect” the files, then just ascend to the directory containing the project dir (cd ..) and use rm -r without -f?
If you’re nervous about rm, there’s many alternatives that work by moving a file to your recycling bin instead of deleting it outright. I think the current fun one is trash-rs, but some distros package trash-cli.
This will print out all the rm -fv commands that would be run. It's basically rm -rf --dry-run, but rm doesn't have that common option. Once you've verified that that's what you want to do, run it again without echo to do the actual deletion. If you're scared of having that in your history, either use a full path for .git, or prepend a space to the non-echo version of the command to make it avoid showing up in your shell history (assuming you have ignorespace in your HISTCONTROL env var)
I use this xargs echo pattern a lot when I'm crafting commands that are potentially destructive or change lots of things.
honestly I don't think there is a better way, like others have said you can use a trash program or you can chmod the git directory before deleting but, I would recommend against the comments saying alias the command, that can lead to even bigger problems if you typo thr alias or mess up in the script. rf can't break anything unless you say the wrong directory which would be the same with aliases anyway,
My recommendation out of them all would be using a trash program to move it to the trash that way if you do screw up the location you have a way to restore it otherwise you could make a script to list the files affected using ls and then prompt a yes/no prompt using read before doing the rm script, but that's something you definitely want to test in a sandbox or user restricted environment if you're not used to scripting in case something breaks