永久删除git历史提交中的大文件

直接清除

安装好python3之后执行

1
2
3
4
5
pip install git-filter-repo
# 删除所有大于 10M的文件
git filter-repo --strip-blobs-bigger-than 10M --force
# 删除某个具体的文件
git filter-repo --path path/to/file.ext --invert-paths

找到 .git\filter-repo\commit-map 文件,备份一下

1
2
3
4
git config --unset remote.origin.mirror
git push origin --force 'refs/heads/*'
# git push origin --force 'refs/tags/*'
git push origin --force 'refs/replace/*'

到 gitlab的 Repository cleanup 上传

Gitlab 说明文档

找到大文件(git < 2.40)

1
2
3
4
5
6
7
git commit -am "commit all"
git gc
git count-objects -v
# 第三列是大小:SHA-1 type size size-in-packfile offset-in-packfile
git verify-pack -v .git/objects/pack/pack-xxxx.idx | sort -k 3 -n | tail -3
# windows
git verify-pack -v .git/objects/pack/pack-xxxx.idx >> 1.txt

根据sha-1 列出所有commit

1
2
3
git rev-list --objects --all |grep b8b2c97e4327aadb5d5b5926a2a8aaa8dc0c5b1f
# windows
git rev-list --objects --all |findstr “b8b2c97e4327aadb5d5b5926a2a8aaa8dc0c5b1f”

移除

1
2
3
4
5
6
git log --pretty=oneline --branches -- docs/.vuepress/public/nacos/nacos-server-2.2.0.1.tar.gz
# 输出
# 91b26ddfe8a744d266498fc6d176b7b75f0113f2 docs:文档
# ea773ea3347b48d132c0ddfa1ae30541c831ed1e docs:build 部署中间件内容迁移
# 删除所有commit
git filter-branch --index-filter 'git rm --cached --ignore-unmatch docs/.vuepress/public/nacos/nacos-server-2.2.0.1.tar.gz' -- ea773ea3347b48d132c0ddfa1ae30541c831ed1e..

删除空间引用

1
2
3
rm -rf .git/refs/original
rm -rf .git/logs/
git gc

查看空间

1
git count-objects -v

本文地址: https://github.com/maxzhao-it/blog/post/d3a05225/