Unable to remove files recursively from Git
Asked Answered
L

4

39

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?

--

Luxuriant answered 28/6, 2009 at 2:3 Comment(0)
L
47
 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
Leanora answered 28/6, 2009 at 13:25 Comment(2)
Does this also remove from git any historical trace of those files from earlier versions?Limassol
@CraigHicks No it does not. For that, you would need git filter-branch (git-scm.com/docs/git-filter-branch) or BFG: rtyley.github.io/bfg-repo-cleanerLeanora
Z
10

You want to remove them even if there are local modifications?

git rm -rf bin/*

Or do you want to remove from the index but keep the files themselves?

git rm -r --cached bin/*

Also try out:

git help rm
Zion answered 28/6, 2009 at 2:8 Comment(2)
@Pate: I want to remove the files from Git such that I have the files in my machine after the removal.Erymanthus
This answer is the foundation of the changed question.Erymanthus
S
1

You should understand what * does a bit first.

Applications don't see * (or other globbing characters) -- they receive all of the matches of the glob as individual arguments.

To understand this better, put echo in front of your first command and see what it prints out:

 git rm -r --cached ~/.vim/*

You'll see each individual match, including things that the program doesn't know how to operate on (which includes .vim/colors).

Stella answered 28/6, 2009 at 21:54 Comment(1)
I feel that this answer should have included the described echo, as well as a solution to the actual problem the OP has.Cooker
A
1

Or it could be that the directory you're trying to recursively remove is in the .gitignore list. I just encountered this. I have ./vendors in my ignore list, and there are a bunch of directories under ./vendors but because anything in vendors is ignored, it's not actually deleting anything like ./vendors/assetic because it's not actually in the repo. I forgot that it was in the ignore list :)

Anthroposophy answered 18/8, 2012 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.