背景
在commit时不小心把上百M的数据集加入了, 导致.git文件夹过大,需要将它们从各个分支除去.
探索过程
删除 git 仓库中无用大文件一文中提到使用git verify-pack -v .git/objects/pack/pack-*.idx
命令列出各种对象, 但我在pack
文件夹内找不到*.idx
文件, 查到fan0128说运行git gc
可以让文件显示, 但我没尝试这个做法.
看到网上有人说bfg清理工具好用, 于是运行了下马上解决了
解决
解决方案跟随BFG Repo-Cleaner的做即可.
第一次运行时, 反馈说有些commit被保护了, 于是查阅How to remove a protected commit using BFG,得知加上--no-blob-protection
参数即可.
E:\codes>java -jar bfg-1.14.0.jar -b 2M mmagnn/.git
Using repo : E:\codes\mmagnn\.git
Scanning packfile for large blobs: 32
Scanning packfile for large blobs completed in 21 ms.
Found 1 blob ids for large blobs - biggest=1007936215 smallest=1007936215
Total size (unpacked)=1007936215
Found 11 objects to protect
Found 7 commit-pointing refs : HEAD, refs/heads/master, refs/heads/pre_sample, ...
Protected commits
-----------------
These are your protected commits, and so their contents will NOT be altered:
* commit 91a70a5b (protected by 'HEAD')
Cleaning
--------
Found 4 commits
Cleaning commits: 100% (4/4)
Cleaning commits completed in 23 ms.
BFG aborting: No refs to update - no dirty commits found??
第二次运行时, 加上了--no-blob-protection
参数, 于是相应的大文件被删除了
E:\codes>java -jar bfg-1.14.0.jar -b 2M mmagnn/.git --no-blob-protection
Using repo : E:\codes\mmagnn\.git
Scanning packfile for large blobs: 32
Scanning packfile for large blobs completed in 20 ms.
Found 1 blob ids for large blobs - biggest=1007936215 smallest=1007936215
Total size (unpacked)=1007936215
Found 0 objects to protect
Found 7 commit-pointing refs : HEAD, refs/heads/master, refs/heads/pre_sample, ...
Protected commits
-----------------
You're not protecting any commits, which means the BFG will modify the contents of even *current* commits.
This isn't recommended - ideally, if your current commits are dirty, you should fix up your working copy and commit that, check that your build still works, and only then run the BFG to clean up your history.
Cleaning
--------
Found 4 commits
Cleaning commits: 100% (4/4)
Cleaning commits completed in 59 ms.
BFG aborting: No refs to update - no dirty commits found??
然后清理一下, 就会发现.git文件夹很小了, git push
即可
E:\codes\mmagnn>git reflog expire --expire=now --all && git gc --prune=now --aggressive
Enumerating objects: 26, done.
Counting objects: 100% (26/26), done.
Delta compression using up to 12 threads
Compressing objects: 100% (23/23), done.
Writing objects: 100% (26/26), done.
Total 26 (delta 9), reused 14 (delta 0), pack-reused 0
E:\codes\mmagnn>git push
Everything up-to-date
清理后的文件夹清清爽爽.