【问题标题】:Unable to remove files recursively from Git无法从 Git 递归删除文件
【发布时间】:2010-11-06 10:40:32
【问题描述】:

我想从 Git 中删除 ~/bin/ 的所有文件。

我跑

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

我明白了

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

此错误消息建议我使用以下 PATH,因为 ~/.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 中的所有文件和子目录?

--

【问题讨论】:

    标签: git rm


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你应该先了解*做了什么。

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

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

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

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

      【讨论】:

      • 我觉得这个答案应该包括所描述的回声,以及OP实际问题的解决方案。
      【解决方案3】:
       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
      

      【讨论】:

      【解决方案4】:

      即使有本地修改,您也要删除它们?

      git rm -rf bin/*
      

      或者您想从索引中删除但保留文件本身?

      git rm -r --cached bin/*
      

      也可以试试:

      git help rm
      

      【讨论】:

      • @Pate:我想从 Git 中删除文件,以便在删除后将文件保存在我的机器中。
      • 这个答案是改变问题的基础。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 2011-01-02
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多