Search code examples
gitrm

Unable to remove files recursively from Git


I want to remove all files from Git at ~/bin/.

I run

git rm -r --cached ~/.vim/*                      # Thanks to Pate in finding --cached!

I get

fatal: pathspec '.vim/colors' did not match any files

This error messsage suggests me to use the following PATHs, since ~/.vim/** does not work

~/.vim/*        # I get the error
~/.vim/*/*/*    # This removes files from the index at ~/.vim/folderA/folderB/file1.txt
~/.vim/*/*      # similar error as to the first PATH

How can you remove all files and subdirectories at ~/.vim from Git?

--


Solution

  •  git rm -r --cached ~/.vim/*   
     fatal: pathspec '.vim/colors' did not match any files
    

    1/ You do not need the '*':

     git rm -r --cached ~/.vim
    

    will take care of any tracked sub-files.

    2/ fatal: pathspec '.vim/colors' did not match any files simply means one of your commands you tried before the one listed in 1/ has worked, and there is no more file to delete!

    # to test that command, first reinitialize the state of the repository
    # save first if you have any other current modifications
    $ git reset --hard
    
    # then check the rm works
    $ git rm -r --cached ~/.vim
    rm '.vim/aPath/aFile1'
    rm '.vim/aSecondPath/aFile2'
    rm '.vim/aThirdPath/aFile3'
    
    # try it again
    $ git rm -r --cached ~/.vim
    fatal: pathspec '.vim/colors