Git 导致 clone 下来时 .git 文件夹特别大,本文介绍如何清除较大的垃圾。

查找大文件

git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 5 -n | tail -5 | awk '{print$1}')"

git verify-pack -v .git/objects/pack/*.idx | sort -k 5 -n | tail -5
1eb97591dc778a28cd2e6aa950d3efd4dd5ae008 download/grafana/grafana-4.0.1-1480694114.x86_64.rpm
09fa9ce5ee85e520185d1c3f2ac0b65c9e7817b4 download/heka/releases/v0.10.0/heka-0_10_0-linux-amd64.rpm
ebeda6f356beb36b001e52f5848b7052384318a3 download/heka/releases/v0.10.0/heka_0.10.0_amd64.deb

移除对该文件的引用

Administrator@scloud MINGW64 /d/code/github/xiexianbin/xiexianbin.github.io (master)
$ git filter-branch --force --index-filter "git rm --cached --ignore-unmatch 'download/heka/releases/v0.10.0/heka-0_10_0-linux-amd64.rpm'"  --prune-empty --tag-name-filter cat -- --all
Rewrite 52bdedbd84d919e9f97f8fd3fe983f963fc914b5 (580/1848) (646 seconds passed, remaining 1412 predicted)    rm 'download/heka/releases/v0.10.0/heka-0_10_0-linux-amd64.rpm'
Rewrite 0006c90f1a63eee315f66b7c8ab053106f4570c1 (1848/1848) (2046 seconds passed, remaining 0 predicted)
Ref 'refs/heads/master' was rewritten
Ref 'refs/remotes/origin/master' was rewritten
WARNING: Ref 'refs/remotes/origin/master' is unchanged
WARNING: Ref 'refs/remotes/origin/v2.0' is unchanged
WARNING: Ref 'refs/tags/v1.0.20150820' is unchanged
WARNING: Ref 'refs/tags/v1.0.20150912' is unchanged
WARNING: Ref 'refs/tags/v1.0.20151018' is unchanged
WARNING: Ref 'refs/tags/v1.0.eol' is unchanged
Ref 'refs/tags/v2.0.1.eol' was rewritten
Ref 'refs/tags/v2.0.2.eol' was rewritten
WARNING: Ref 'refs/tags/v2.0.eol' is unchanged
v1.0.20150820 -> v1.0.20150820 (1151f5626c1e478e6171913590f5fec6e8ea98b9 -> 1151f5626c1e478e6171913590f5fec6e8ea98b9)
v1.0.20150912 -> v1.0.20150912 (d983fc2861533698f74e56599a290cedafc83896 -> d983fc2861533698f74e56599a290cedafc83896)
v1.0.20151018 -> v1.0.20151018 (fb0830858f2c8775de5eeadb2fce7ee11f1d280d -> fb0830858f2c8775de5eeadb2fce7ee11f1d280d)
v1.0.eol -> v1.0.eol (e88f336910d0eb78dd1def56ac570e528f23dbfa -> e88f336910d0eb78dd1def56ac570e528f23dbfa)
v2.0.eol -> v2.0.eol (fe46df49bdc9392d27a582e03ddc81a93e0833d2 -> fe46df49bdc9392d27a582e03ddc81a93e0833d2)

Administrator@scloud MINGW64 /d/code/github/xiexianbin/xiexianbin.github.io (master)
$

进行 repack

git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --auto
git gc --prune=now
git repack -d -l

GC

git push --force --verbose --dry-run
git push --force

完毕。