Git
导致 clone
下来时 .git
文件夹特别大,本文介绍如何清除较大的垃圾。
仓库瘦身
注意:
- 该操作仅对
当前分支
的文件有效,不影响其他分支
- 操作前建议先打
tag
,该操作不会影响已有 tag
查找大文件
查找大文件
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
脚本作用:
- 倒序列出的所有提交对象的
id
- 读取
git
归档后的 idx
文件,列出所有的对象列表,size
进行升序排序,然后取最后 5
项,截取第一列(SHA-1)
- 取
脚本一
和脚本二
的交集,便是当前仓库中占用空间最大的5
个文件
或提交记录
或
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
移除对该文件的引用
删除冗余文件
以及相关提交记录
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch 文件名' \
--prune-empty -- --all
git filter-branch --force --index-filter \
'git rm -r --cached --ignore-unmatch 文件夹名' \
--prune-empty -- --all
注意:
$ 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)
GC and push
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
git push --force --verbose # --dry-run
查找删除过的文件
git log --diff-filter=D --summary <dir>
然后可以使用上面的删除命令进行移除对文件的引用。
删除所有 Commit 记录
git checkout --orphan latest_branch
git add -A
git commit -am "commit message"
git branch -D master
git branch -m master
git push -f origin master