下面演示的方式就是如何彻底清除git中的大文件或隐私文件。
查询git中的大文件记录
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
部分git项目会提示:
Cannot open existing pack file '.git/objects/pack/*.idx'
说明该项目并未触发git的packfile机制,可以忽略。
使用BFG Repo-Cleaner
BFG Repo-Cleaner是一个替代git原生命令git-filter-branch的解决git历史记录问题的方案,BFG Repo-Cleaner的使用更加方便简洁。项目官网:
https://rtyley.github.io/bfg-repo-cleaner/
下载到本地保存为bfg.jar,保证本地的java运行环境可用。
clone项目
git clone --mirror git@github.com:kanchuan/test.git
注意:一定要使用--mirror参数。
标记删除文件
java -jar bfg.jar --delete-files (filename) test.git #指定文件删除
java -jar bfg.jar --strip-blobs-bigger-than 100M test.git #条件删除
The BFG will update your commits and all branches and tags so they are clean, but it doesn't physically delete the unwanted stuff. Examine the repo to make sure your history has been updated, and then use the standard git gc command to strip out the unwanted dirty data, which Git will now recognise as surplus to requirements:
删除文件
cd test.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
推送到远程
git push
留言板