如何删除git远程仓库中所有被忽略的文件?

2023年2月21日服务端评论31,418字数 378阅读1分15秒阅读模式

如果之前已经push到远程仓库的文件,即使之后把文件加入了.gitignore文件,还是会提交修改,无法被忽略。

有没有什么办法删除git远程仓库中所有被忽略的文件呢?

有一个命令可以解决这个问题:

git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached

它的作用是:

  • 列出所有被忽略的文件
  • 处理带有空格的路径以避免失败
  • 调用git rm -r --cached从索引中删除所有被忽略的文件(不从本地计算机中删除它们)

对于 2022 年使用此功能的任何人,还需要给 ls-files 增加 -c 参数:

git ls-files -c --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached

Git 的基础使用及命令汇总 服务端

Git 的基础使用及命令汇总

仅自用,内容仅仅保证自己看得懂 常用基础命令 git init - 初始化仓库 git clone - 拷贝远程仓库 git status - 显示变更 git add . - 添加文件到暂存区 gi...
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定