Skip Navigation

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?

43 comments
  • Using rm -rf scares me. Is there a reasonable way to delete git repos without it?

    I don't know what to tell you, that's the command you need to use.

    If you're that worried you're going to nuke important stuff, make backups, and don't use sudo for user files.

  • 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.

  • The problem is that rm -rf shouldn't scare you?

    What are the chances something like

     undefined
        
    ~/projects/some-project $ cd ..
    ~/projects $ rm -fr some-project
    
      

    may delete unexpected stuff? (especially if you get into the habit of tab-completing the directory argument)

  • 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?

    The force flag (-f) is the scary one, I presume?

  • 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

43 comments