无法从 Git 递归删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1054044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:39:07  来源:igfitidea点击:

Unable to remove files recursively from Git

gitrm

提问by Léo Léopold Hertz ??

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

我想从 Git 中删除 ~/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/** 不起作用

~/.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?

如何从 Git 中删除 ~/.vim 中的所有文件和子目录?

--

——

回答by VonC

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

1/ You do not need the '*':

1/ 你不需要' *':

 git rm -r --cached ~/.vim

will take care of any tracked sub-files.

将处理任何跟踪的子文件。

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

2/fatal: pathspec '.vim/colors' did not match any files仅表示您在 1/ 中列出的命令生效之前尝试过的命令之一,并且没有更多文件要删除!

# 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

回答by Léo Léopold Hertz ??

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

回答by targnation

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 :)

或者可能是您尝试递归删除的目录在 .gitignore 列表中。我刚遇到这个。我的忽略列表中有 ./vendors ,并且 ./vendors 下有一堆目录,但是因为 vendor 中的任何内容都被忽略,所以实际上并没有删除 ./vendors/assetic 之类的内容,因为它实际上不在回购中。我忘了它在忽略列表中:)

回答by Dustin

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 echoin front of your first command and see what it prints out:

为了更好地理解这一点,把echo你的第一个命令放在前面,看看它打印出什么:

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

您将看到每个单独的匹配项,包括程序不知道如何操作的内容(包括.vim/colors)。