如何列出仅被.git/info/exclude规则匹配的Git忽略文件
解决方案
高效原生Git实现
你可以用Git内置的文件列表能力替代系统find遍历,充分利用Git的索引缓存大幅提升运行速度,命令如下:
git ls-files --others | git check-ignore -v --stdin | grep '\.git/info/exclude'
命令逻辑说明
git ls-files --others直接输出仓库内所有未被Git跟踪的文件,基于Git的本地索引缓存读取,无需遍历文件系统,执行效率远高于系统find命令- 输出通过管道传递给
git check-ignore -v --stdin,批量校验每个文件匹配的忽略规则来源,--stdin参数支持批量读取路径,避免单文件校验的额外开销 - 最后通过
grep筛选出规则来源为.git/info/exclude的条目,就是你需要找的私有忽略文件列表
如果你只需要输出纯文件路径方便后续清理,可以加awk处理输出字段:
git ls-files --others | git check-ignore -v --stdin | grep '\.git/info/exclude' | awk '{print $4}'
需要直接清理这些文件的话,追加删除逻辑即可:
git ls-files --others | git check-ignore -v --stdin | grep '\.git/info/exclude' | awk '{print $4}' | xargs rm -f
内容的提问来源于stack exchange,提问作者blueFast
相关产品推荐
相关产品推荐

