You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Git历史中彻底移除所有__pycache__文件夹?

彻底移除Git历史中所有目录下的__pycache__文件夹

你之前的命令仅匹配根目录下的__pycache__/,要覆盖所有层级子目录,需调整路径匹配规则。另外Git官方已弃用git filter-branch,更推荐使用高效可靠的git filter-repo工具,以下是两种可行方案:

方案一:修正git filter-branch命令

执行以下命令,通过**/__pycache__/递归匹配所有目录下的缓存文件夹:

git filter-branch -f --index-filter 'git rm -rf --cached --ignore-unmatch **/__pycache__/' HEAD

方案二:使用git filter-repo(推荐)

  1. 先安装git filter-repo:

    • Debian/Ubuntu系统:sudo apt install git-filter-repo
    • macOS系统:brew install git-filter-repo
    • 其他系统可通过官方源码编译安装
  2. 执行过滤命令移除所有__pycache__文件夹:

git filter-repo --path-glob '**/__pycache__/' --invert-paths

后续操作

  1. 将__pycache__加入.gitignore,避免后续误提交:
echo "__pycache__/" >> .gitignore
echo "**/__pycache__/" >> .gitignore
git add .gitignore
git commit -m "Ignore all __pycache__ directories"
  1. 强制推送重写后的历史到远程仓库(注意:若为多人协作仓库,需提前通知所有成员重新克隆仓库,防止历史冲突):
git push origin --force --all
git push origin --force --tags

内容的提问来源于stack exchange,提问作者Michael Moreno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 17:20:02