Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
git rm--缓存.gitignore中列出的所有内容_Git_Git Rm - Fatal编程技术网

git rm--缓存.gitignore中列出的所有内容

git rm--缓存.gitignore中列出的所有内容,git,git-rm,Git,Git Rm,在我把所有东西都提交后,我在我的项目中添加了.gitignore。现在我想运行如下命令: git rm --cached *everything_listed_in_gitignore* 如何做到这一点?提前谢谢。我总是使用以下行删除my中列出的文件。gitignore: grep -vP "^(#|\$)" .gitignore|sed "s/^\///"|xargs rm -f 这将发现.gitignore中的行与正则表达式^(#| \$)中以开头的行(注释)或空行不匹配(-v选项到gr

在我把所有东西都提交后,我在我的项目中添加了.gitignore。现在我想运行如下命令:

git rm --cached *everything_listed_in_gitignore*

如何做到这一点?提前谢谢。

我总是使用以下行删除my
中列出的文件。gitignore

grep -vP "^(#|\$)" .gitignore|sed "s/^\///"|xargs rm -f
  • 这将发现
    .gitignore
    中的行与正则表达式
    ^(#| \$)
    中以
    开头的行(注释)或空行不匹配(
    -v
    选项到grep)

  • 然后
    sed
    将删除行首的正斜杠
    /

  • 使用
    xargs
    将结果传递给
    rm-f
    ,您可以用
    git-rm--cached

  • 注意:


    您的
    .gitignore
    文件可以包含类似
    *.tmp
    的条目,以忽略整个项目中的所有
    .tmp
    文件。上述命令只会从根目录中删除
    *.tmp
    文件。只有完整路径才能正确处理。

    我找到了另一种方法,它在任何操作系统中都能更好地工作

    将新规则添加到.gitignore后

    git rm -r --cached .
    
    git add .
    

    基本上,它删除所有内容并将所有内容重新添加。添加时,Git将忽略基于.gitignore的文件。有一个较短的版本,答案取自

    git rm -r --cached .
    
    git add .
    
    您还可以基于.gitignore从存储库中删除文件,而无需从本地文件系统中删除它们:

    git rm --cached `git ls-files -i -X .gitignore`
    
    或者,在Windows Powershell上:

    git rm --cached $(git ls-files -i -X .gitignore)
    

    如果macOS标准
    grep
    上缺少
    -P
    选项,请使用安装一个。