git瘦身:清除大文件或敏感文件记录

 原创    2019-04-23

使用git进行版本管理的项目随着提交代码次数增多,.git目录会逐渐增大。特别是由于没有及时添加进.gitignore的大文件会显著增加.git的大小。另外,由于一时疏忽而上传的敏感文件,即使从工作目录清除还是能在历史记录中查看到。

下面演示的方式就是如何彻底清除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

github access tokens

相关文章:

git config 配置
github access tokens
git技巧:gitignore忽略已提交的文件

发表留言

您的电子邮箱地址不会被公开,必填项已用*标注。发布的留言可能不会立即公开展示,请耐心等待审核通过。