赞
踩
├── HEAD ├── branches ├── index ├── logs │ ├── HEAD │ └── refs │ └── heads │ └── master ├── objects │ ├── 88 │ │ └── 23efd7fa394844ef4af3c649823fa4aedefec5 │ ├── 91 │ │ └── 0fc16f5cc5a91e6712c33aed4aad2cfffccb73 │ ├── 9f │ │ └── 4d96d5b00d98959ea9960f069585ce42b1349a │ ├── info │ └── pack └── refs ├── heads │ └── master └── tags
每次git-add都会生成一个Git对象,称为 blob对象,存放在objects目录下。
这个blob 对象里保存的是什么呢?
Git在add文件时,会把文件完整的保存成一个新的 blob 对象。通过 git-gc打包或者每次git-push的时候Git都会自动执行一次打包过程,将blob对象合并成一个包文件,同时会生成一个索引文件,索引文件中包含了每个blob对象在包文件中的偏移信息,Git在打包的过程中使用了增量编码方案,只保存blob对象的不同版本之间的差异,这使得仓库会瘦身。
既然Git会对Blob对象进行合并优化,那么objects文件夹为什么还会那么大呢?
因为当Blob对象在合并时不能对.a进行差异化比较,所以每次在添加.a文件时,都会保存一份.a文件,用于后续代码还原时使用。
所以当频繁更换.a文件时,objects下的pack文件会越来越大。虽然这个.a文件后续可能用不到删除了,但是pack中的这个.a文件的缓存还是会一直存在。
删除pack中无用的大文件缓存
1、首先先找出git中较大的五个提交记录文件:
git verify-pack -v .git/objects/pack/pack-*.idx | sort -k 3 -g | tail -5
如果想查看所有的提交记录可执行一下命令:
git rev-list --all
执行结果(当前我们以查询最大的五次做介绍):
dbad6eb20d31a5aefe132b74b2137cd10105c574 blob 16684712 7287784 86721282
6c858bc93421b2db41dafc2bfd4eb82c77c50266 blob 17504576 8257957 47764046
416088453a2514ada98ba639af3ff298510b4246 blob 22216104 10854126 75719527
f0d8d3b476526af42b4e06f390a1b4925580e99b blob 22435760 10589160 16678516
553ba826b92c9d42acb1e586774ac697661588c9 blob 29814552 12998052 60871184
2、第一行的字母其实相当于文件的id,用以下命令可以找出id对应的文件名:
git rev-list --objects --all | grep dbad6eb20d31a5aefe132b74b2137cd10105c574
执行结果:
dbad6eb20d31a5aefe132b74b2137cd10105c574 Pods/UMengUShare/UShareSDK/SocialLibraries/WeChat/WechatSDK/libWeChatSDK.a
3、找到最大的几个文件后,怎么删除呢?
能够胜任这个任务的命令叫做 filter-branch:
git filter-branch --force --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch Pods/UMengUShare/UShareSDK/SocialLibraries/WeChat/WechatSDK/libWeChatSDK.a' --tag-name-filter cat -- --all
4、上面代码执行完毕后有可能会报以下错误
WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites. Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as 'git filter-repo'
(https://github.com/newren/git-filter-repo/) instead. See the
filter-branch manual page for more details; to squelch this warning,
set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...
Cannot rewrite branches: You have unstaged changes.
5、如果出现以上错误可执行以下命令 (不报错则跳过此步)
git stash
然后重新执行移除命令
git filter-branch --force --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch Pods/UMengUShare/UShareSDK/SocialLibraries/WeChat/WechatSDK/libWeChatSDK.a' --tag-name-filter cat -- --all
执行结果:
WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites. Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as 'git filter-repo'
(https://github.com/newren/git-filter-repo/) instead. See the
filter-branch manual page for more details; to squelch this warning,
set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...
Rewrite 967ff01a8b2bc7d7f6c90630ede38a2135ba5813 (1/41) (0 seconds passed, remaiRewrite 9674de7f9f8ed029785421f2246bb9d41786cc49 (2/41) (0 seconds passed, remaiRewrite e04ab331167f24b7d26d69e169386e690081af74 (3/41) (0 seconds passed, remaiRewrite 997e93fdaff75162eda8e5c6ee6d3265ce61fe96 (4/41) (0 seconds passed,
Rewrite 429d321e2ee4c54d8d96b35a8e35d021bd8930f1 (35/41) (3 seconds passed, remaining 0 predicted) rm 'Pods/UMengUShare/UShareSDK/SocialLibraries/WeChat/WechatSDK/libWeChatSDK.a'
Ref 'refs/heads/master' was rewritten
Ref 'refs/remotes/origin/master' was rewritten
WARNING: Ref 'refs/remotes/origin/master' is unchanged
Ref 'refs/stash' was rewritten
6、等命令执行完后,要提交到远程:
git push --force --all
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。