Git 仓库瘦身与 LFS 大文件存储

news2024/10/6 20:37:20

熟悉 Git 的小伙伴应该都知道随着 Git 仓库维护的时间越来越久,追踪的文件越来越多,git 存储的 objects 数量会极其庞大,每次从远程仓库 git clone 的时候都会墨迹很久。如果我们不小心 git add 了一个体积很大的文件,且 git push 到了远程仓库,那么我们 git clone 的时候也会很慢。

看一下 GitHub 上的 microsoft/vscode 仓库,都有 九万多个 commit 了,可想而知 objects 的数量应该很恐怖,尝试 clone 一下(一百多万个 objects):

github vscode repo

clone vscode repository

这里微微记录下 Git 仓库瘦身和使用 Git LFS 进行大文件存储管理的几个常规操作。

Git 仓库瘦身

瘦身背景:错误把大文件 push 到了远程仓库

我们可以通过以下命令或者 du -mh 查看 Git 仓库的体积,git-count-objects:

# 查看仓库体积情况
git count-objects -vH

示例:可以看到当前仓库体积只有 12.00 KiB 左右

demo git repository

现在我们模拟错误的将大文件上传到远程 Git 仓库的动作:

# 1、生成一个 90MB 大小的文件,Github 做了限制超过 100 MB 大小的文件建议使用 LFS,直接拒绝 push
➜ dd if=/dev/zero of=bigfile bs=90MB count=1
# 2、将这个文件 push 到远程仓库
➜ git add bigfile
➜ git commit -m "add 90MB bigfile"
➜ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 85.71 KiB | 306.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: warning: See http://git.io/iEPt8g for more information.
remote: warning: File bigfile is 85.83 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB
remote: warning: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
To github.com:yeshan333/git-lfs-prune-repo.git
   e3baf1a..f057313  master -> master

好,接下来我们假装这个仓库有很多文件,不知道具体是那个文件让 Git 仓库的体积突然变大,导致 clone 很慢🤣。就算知道了是哪里个文件造成的,我们直接删除那个文件是没有用的,我们还需要删除那个文件对应的 Git Object 文件

接下来我们可以通过一下命令将本地 clone 的仓库历史提交过的体积较大的前 5 个文件名与对应的 Object 文件的 ID 罗列出来:

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

然后我们删除历史提交过的大文件 bigfile,从日志中我们可以看到本地仓库已经移除大文件成功了

➜ git filter-branch --force --index-filter 'git rm -rf --cached --ignore-unmatch bigfile' --prune-empty --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 e3baf1ac709ae54b60afac9038adcf26fd086748 (1/1) (0 seconds passed, remaining 0 predicted)
WARNING: Ref 'refs/heads/master' is unchanged
WARNING: Ref 'refs/remotes/origin/master' is unchanged
WARNING: Ref 'refs/remotes/origin/main' is unchanged
WARNING: Ref 'refs/remotes/origin/master' is unchanged

接下来我们使用 reflog 和 gc 压缩(清理和回收大文件占用的 objects 空间)看看瘦身效果,最后将变动推送到远程仓库即可:

➜ git reflog expire --expire=now --all && git gc --prune=now --aggressive

➜ git count-objects -vH

➜ git push --mirror
Total 0 (delta 0), reused 0 (delta 0)
To github.com:yeshan333/git-lfs-prune-repo.git
 - [deleted]         main
 + f057313...e3baf1a master -> master (forced update)
 * [new branch]      origin/HEAD -> origin/HEAD
 * [new branch]      origin/main -> origin/main
 * [new branch]      origin/master -> origin/master

What's the difference between git clone --mirror and git clone --bare

Git LFS 大文件存储

如果我们之前生成的大文件 bigfile 大小超过 100 MB,那么 push 到 Github 的时候,会抛出个 error 错误,并会有条建议使用 LFS (Large File Storage):https://git-lfs.github.com/ 管理这个大文件:

➜ git push origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 1.85 MiB | 752.00 KiB/s, done.
Total 3 (delta 0), reused 1 (delta 0)
remote: error: Trace: 993cb74d30fdb2342e7243f5a7002c1892d00d3a216b80e64b43ef7e4382b947
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File bigfile is 1907.35 MB; this exceeds GitHub's file size limit of 100.00 MB
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
To github.com:yeshan333/git-lfs-prune-repo.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'git@github.com:yeshan333/git-lfs-prune-repo.git'
# 仓库初始化 LFS
➜ git lfs install
Updated git hooks.
Git LFS initialized.
# 创建大文件
➜ dd if=/dev/zero of=bigfile200 bs=200MB count=1
1+0 records in
1+0 records out
200000000 bytes (200 MB, 191 MiB) copied, 0.176594 s, 1.1 GB/s
# 指定 LFS 追踪大文件
➜ git lfs track "bigfile200"
Tracking "“bigfile200”"
# 被追踪的文件会记录再 .gitattributes 文件中我们将 .gitattributes 文件 push 到远程仓库即可
➜ cat .gitattributes
"bigfile200" filter=lfs diff=lfs merge=lfs -text
➜ git add .gitattributes
➜ git commit -m "add .gitattributes"
➜ git push

# 提交大文件
➜ git add bigfile200
➜ git commit -m "bigfile 200MB"
[master 84fb90b] bigfile 200MB
 1 file changed, 3 insertions(+)
 create mode 100644 bigfile200
➜ git push
Uploading LFS objects: 100% (1/1), 200 MB | 3.7 MB/s, done.
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 423 bytes | 423.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:yeshan333/git-lfs-prune-repo.git
   aef9a0b..84fb90b  master -> master

开启了 LFS 之后,对应大文件的内容存储在 LFS 服务器中,不再是存储在 Git 仓库中,Git 仓库中存储的是大文件的指针文件,LFS 的指针文件是一个文本文件。

Done?

参考

  • Push Mirroring-Gitlab
  • git 瘦身 | Palance's Blog
  • 详解 Git 大文件存储(Git LFS)

    本文由博客群发一文多发等运营工具平台 OpenWrite 发布

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1526531.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

通俗易懂的精度Precision和召回率Recall解释,看这篇就行,5分钟记住。

一、背景 因为我是做机器人方向的,不可避免的涉及到视觉方向的内容,还有审稿的时候也会看到识别相关的内容,其中衡量识别效果的指标包括精度Precision和召回率Recall,虽然很好理解,但每次都记不住,趁这次机…

【递归专题】【蓝桥杯备考训练】:有序分数、正则问题、带分数、约数之和、分形之城【已更新完成】

目录 1、有序分数(usaco training 2.1) 2、正则问题(第八届蓝桥杯省赛C A组 & Java A组) 3、带分数(第四届蓝桥杯省赛Java A组/B组 & C B组/C组) 4、约数之和(《算法竞赛进阶指南》…

Flink实时数仓之用户埋点系统(二)

用户埋点平台-数仓建模 1、数据仓库 1.1 数据仓库的构建 1.1.1 数据模型 数据模型就是数据组织和存储方法,它强调从业务、数据存取和使用角度合理存储数据。只有将数据有序的组织和存储起来之后,数据才能得到高性能、低成本、高效率、高质量的使用。…

【ArcGISProSDK】获取扩展模块许可到期时间

结果 以下是获取的3D分析模块的许可到期时间 代码 var licenseExpirationDate ArcGIS.Core.Licensing.LicenseInformation.GetExpirationDate(LicenseCodes.Analyst3D); 扩展模块 MemberDescriptionAnalyst3D3D AnalystAviationAirportsAviation and AirportsBusinessAnal…

数目之差

解法一&#xff1a; 显然只需让多的在限度内最多即可 #include<iostream> #include<algorithm> using namespace std; #define endl \n void solve() {int n, k, num0 0, num1 0;cin >> n >> k;string s;cin >> s;for (int i 0; i < s.s…

OpenAI Q-Star:AGI距离自我意识越来越近

最近硅谷曝出一份54页的内部文件&#xff0c;揭露了去年OpenAI宫斗&#xff0c;导致Altman&#xff08;奥特曼&#xff09;差点离职的神秘项目——Q-Star&#xff08;神秘代号Q*&#xff09;。 根据该文件显示&#xff0c;Q-Star多模态大模型拥有125万亿个参数&#xff0c;比现…

linux之source.list解析

众所周知&#xff0c;linux可以通过apt命令安装软件&#xff0c;那么apt又是从哪里获取软件包呢并安装呢&#xff1f;这里就绕不开一个文件source.list&#xff0c;该文件定义了软件源相关的信息。下面以实际例子&#xff0c;详细的介绍下这个文件。 文件作用 定义软件源的信…

就业班 第二阶段 2401--3.18 初识mysql

初识&#xff1a; 1、关系型数据库mysql、mariadb、sqlite 二维关系模型 2、非关系型数据库 redis、memcached sql 四个部分 DDL 数据库定义语言 创建数据库&#xff0c;创建用户&#xff0c;创建表 DML 数据库操作语言 增删改 DQL 数据库查询语言 查 DCL 数据库控制语言 授权 …

phpstudy自定义安装mysql8.3并启动

phpstudy自定义安装mysql8.3并启动 先去官网:https://dev.mysql.com/downloads/下载压缩包文件 然后按下面的图片一步一步操作 选择版本&#xff0c;选择第一个压缩包文件&#xff0c;下载 下载完成后&#xff0c;解压到phpstudy环境目录下&#xff0c;如下图 然后进入mysq…

Ubuntu 搭建gitlab服务器,及使用repo管理

一、GitLab安装与配置 GitLab 是一个用于仓库管理系统的开源项目&#xff0c;使用Git作为代码管理工具&#xff0c;并在此基础上搭建起来的Web服务。 1、安装Ubuntu系统&#xff08;这个教程很多&#xff0c;就不展开了&#xff09;。 2、安装gitlab社区版本&#xff0c;有需…

【每日一题】区域和检索 - 数组不可变

文章目录 Tag题目来源解题思路方法一&#xff1a;自定义前缀和数组方法二&#xff1a;使用 accumulate() 实现前项求和 写在最后 Tag 【前缀和】【数组】【2024-03-18】 题目来源 303. 区域和检索 - 数组不可变 解题思路 方法一&#xff1a;自定义前缀和数组 前缀和的基础知…

第四百零二回

文章目录 知识回顾示例代码经验总结 我们在上一章回中介绍了MethodChannel的使用方法&#xff0c;本章回中将介绍EventChannel的使用方法.闲话休提&#xff0c;让我们一起Talk Flutter吧。 知识回顾 我们在前面章回中介绍了通道的概念和作用&#xff0c;并且提到了通道有不同的…

如何突破DRAM对SSD容量提升的限制?

近日小编看到Pure Storage公司的研发高级副总裁肖恩罗斯马林(Shawn Rosemarin)的一个观点“由于DRAM的局限性&#xff0c;固态硬盘(SSD)的容量难以突破30TB”。 这个观点不是完全准确&#xff0c;实际上&#xff0c;Solidigm已经发布了最大容量61.44TB QLC SSD。 但是&#xf…

AI 初创公司趋势:Y Combinator 最新批次的见解

总部位于硅谷的著名创业加速器 Y Combinator (YC) 最近宣布了其 2023 年冬季队列&#xff0c;不出所料&#xff0c;约 31% 的初创公司&#xff08;269 家中有 80 家&#xff09;拥有自我报告的 AI 标签。在这篇文章中&#xff0c;我分析了这批 20-25 家初创公司&#xff0c;以了…

深度剖析:数字经济下人工智能水平的新测算模型数据集

数据来源&#xff1a;企业年报时间跨度&#xff1a;1991-2022年数据范围&#xff1a;各企业数据指标&#xff1a; 年份 股票代码 公司名称 总词频 词频加1取对数 人工智能 计算机视觉 图像识别 知识图谱 智能教育 增强现实 智能政务 特征提…

Leetcode刷题笔记——动态规划(背包问题)篇

Leetcode刷题笔记——动态规划&#xff08;背包问题&#xff09;篇 一、0-1 背包问题 0-1背包问题简介 有 n 件物品和一个最多能背重量为 w 的背包。第 i 件物品的重量是 weight[i]&#xff0c;得到的价值是value[i] 。每件物品只能用一次&#xff0c;求解将哪些物品装入背包…

HTML学习:图片格式——超链接

一、图片格式 1.jpg格式 概述:扩展名为.jpg 或.jpeg &#xff0c;是一种有损的压缩格式(把肉眼不容易观察出来的细节丢弃了)。 主要特点:支持的颜色丰富、占用空间较小、不支持透明背景、不支持动态图。 使用场景:对图片细节没有极高要求的场景&#xff0c;例如:网站的产品…

容器数据卷

目录 一、容器数据卷概念 二、使用数据卷 2.1直接使用命令来挂载 三、实战测试 四、具名挂载和匿名挂载 4.1匿名挂载举例&#xff1a; 4.2具名挂载举例&#xff1a; 五、数据卷容器 一、容器数据卷概念 数据&#xff1f;如果数据都在容器中&#xff0c;那么容器删除&am…

linux(Ubuntu22) 一篇带你学会Linux,详细篇

Linux 简介 精通Linux&#xff0c;自带python&#xff0c;系统开源 电脑可安装双系统 c盘安装win D盘安装linux 在一套硬件上只能同时运行一个操作系统 虚拟机 模拟真实环境 在虚拟机内运行操作系统 需要硬件支持虚拟化 开启VT-X VM…

Web基础06-AJAX,Axios,JSON数据

目录 一、AJAX 1.概述 2.主要作用 3.快速入门 4.AJAX的优缺点 &#xff08;1&#xff09;优点 &#xff08;2&#xff09;缺点 5.同源策略 二、Axios 1.概述 2.快速入门 3.请求方式别名 三、JSON 1.概述 2.主要作用 3.基础语法 4.JSON数据转换 &#xff08;1…