无法递归地从Git中删除文件

38
我想要从Git中删除~/bin/下的所有文件。
我执行以下命令:
git rm -r --cached ~/.vim/*                      # Thanks to Pate in finding --cached!

我得到

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

这个错误信息建议我使用以下路径,因为~/.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
如何从 Git 中删除 ~/.vim 目录下的所有文件和子目录?
4个回答

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

1/ 您不需要使用 '*' 符号:

 git rm -r --cached ~/.vim

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

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

这是否也会从git中删除早期版本中那些文件的所有历史痕迹? - Craig Hicks
1
@CraigHicks 不,它不具备此功能。为此,您需要使用 git filter-branch (https://git-scm.com/docs/git-filter-branch) 或 BFG 工具: https://rtyley.github.io/bfg-repo-cleaner/。 - VonC

10
你想要删除它们,即使存在本地修改吗?
git rm -rf bin/*

还是您想要从索引中删除但保留文件本身呢?

git rm -r --cached bin/*

还可以尝试:

git help rm

@Pate:我想从Git中删除文件,以便在删除后我可以在我的计算机上拥有这些文件。 - Léo Léopold Hertz 준영
这个答案是修改后问题的基础。 - Léo Léopold Hertz 준영

1

或者你试图递归删除的目录在.gitignore列表中。我刚遇到这个问题。我的忽略列表中有./vendors,而./vendors下有一堆目录,但由于vendors中的任何内容都被忽略了,所以它实际上并没有删除像./vendors/assetic这样的任何东西,因为它实际上不在仓库中。我忘记它在忽略列表中了 :)


1

首先,你应该了解一下 * 的作用。

应用程序看不到 *(或其他通配符字符)-- 它们将 glob 的所有匹配项作为单独的参数接收。

为了更好地理解这一点,在你的第一个命令前加上 echo 并查看它打印出来的内容:

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

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


我认为这个答案应该包括所描述的回声,以及解决OP实际问题的解决方案。 - superbeck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接