惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
小众软件
小众软件
GbyAI
GbyAI
B
Blog RSS Feed
月光博客
月光博客
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
博客园_首页
G
Google Developers Blog

Dvel's Blog

Rime 全拼双拼混输 CHD 油猴脚本:每日签到自动答题 Rime 配置:雾凇拼音 Hammerspoon 自动切换输入法 修复 Hugo 本地图片的累计布局偏移(CLS)问题 macOS 利用 Karabiner 修改 Emacs 键位为 Vim 键位 macOS 修改 Emacs 键位为 Vim 键位 Surge 配置 汉字的混乱 Netflix 中英双语字幕的好办法 优化 Rime 英文输入体验 判断字符是否为简体字或繁体字 Cloudflare 转入域名提示「出现了问题。请重试转移此域。尚未向您收费。」 图片压缩:Squoosh、TinyPNG、ImageOptim、WebP macOS grep + sed 批量替换多个文件的内容 在 macOS 根目录创建文件夹 博客图床小妙招 折腾 Hugo & PaperMod 主题 利用 Cloudflare Workers 进行批量 301 重定向 将博客部署在 Cloudflare Pages Alfred File Navigation(文件导航器)详解 在 Hugo Goldmark Markdown 中设置以新标签打开链接 日期与时间(UTC、GMT、时间戳、时区) 让 Alfred 以新标签的方式打开 Finder qBittorrent 设置教程 联通光猫破解+桥接记录 配置 Mac 终端走代理 Steam for Mac 中文游戏推荐 我的 Mac 软件/工具列表 《Flask Web 开发》笔记
用 git filter-branch 给 Git 仓库瘦身
Dvel · 2021-05-09 · via Dvel's Blog

有时候项目里引用了一些大文件,导致 .git 容量高达几个 G。

如果这些大文件丢失并不敏感,也没什么用处,可以清理掉来节约一下空间。

操作前建议备份。

1. gc

使用 git-gc 将对象打包。

.git 已经从 4 个 G 变成了 322M。

2. 查找最大的 3 个文件

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

4badfe287e3f1d6e4e288b2c3b47261119ce2fca foo/bar1.xxx
00e7645c93c2368b0634bda72507b40c936dd1b3 foo/bar2.xxx
4088cefbbc3dcd6a17dfa656423ad20137fad9dd foo/bar3.xxx

3. 删除文件

$ git filter-branch --force --index-filter "git rm -rf --cached --ignore-unmatch <filename>"  --prune-empty --tag-name-filter cat -- --all

如:

$ git filter-branch --force --index-filter "git rm -rf --cached --ignore-unmatch foo/bar1.xxx"  --prune-empty --tag-name-filter cat -- --all

1000 多个提交大概运行了 2 分钟。

4. 清除缓存

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

5. 查看成果

一个几十 M 的文件清除后,整个包减小了大概 100 M,多清理几个就降下来了。

最后 Push 时需使用 --force 参数覆盖远端。

参考 & 感谢

Git清理删除历史提交文件

如何解决 GitHub 提交次数过多 .git 文件过大的问题?