欢迎大家关注 B 站视频讲解:https://www.bilibili.com/video/BV1GR4y117H4
简介及使用场景
Git LFS(Large File Storage) 可以把音频样本,视频,数据集,相片等大文件替换成文本指针文件,而这些二进制大文件会存储在远程 LFS 服务器上。类似支持 Git LFS 的平台有 GitHub/Gitlab/bitbucket 等。
在实际 Git 工程项目中,会出现 Jenkins git clone 失败,git clone 缓慢等,影响 CI/CD 的执行,我们可以通过 Git LFS 对仓库进行瘦身,提升 CI/CD 的质量效率。
对于人工智能、机器学习的研发来说,音视频,数据集等占用很大的工程存储占比。
对于智能硬件、嵌入式相关的研发来说,.so,.a 等一些 prebuilts 文件会占用很大的存储。
对于前端开发的研发来说,png,jpg 等文件会占用较多存储空间。
还有些常见的文件,如 docx 文档,zip 压缩包等,可能也需要通过 Git 管理起来。
这些非文本文件数量不断增多,版本也经常迭代,本地 Git 仓库占用磁盘空间越来越大,git checkout 命令会变得非常缓慢,而且其他用户 clone 仓库时也会变得异常缓慢。
优势
降低整个仓库的体积,大大提高首次 git clone 的速度;
只有执行 git checkout 分支和 commit 时,才会下载当前需要用到 LFS 文件,减少本地磁盘空间占用。
原理
Git LFS 实现优化大文件存储的方式,是使用文件的指针文件替换原来的大文件,把大文件单独存储于 lfs 缓存目录中。当我们需要用到这些大文件时,Git LFS 自动会根据指针,从缓存目录中检出对应真实文件。
# 文件指针示例
version https://git-lfs.github.com/spec/v1
oid sha256:ecde47ec99907cbbf6a3edc14f2dab74841a2ab3335b22acea0f476be3c3574d
size 553428
安装 git-lfs
xiaobaiyang@ubuntu2004server:~/projects/devops_test_python$ sudo apt install git-lfs
[sudo] password for xiaobaiyang:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
git-lfs
0 upgraded, 1 newly installed, 0 to remove and 7 not upgraded.
使用方式
$ git lfs install
$ git lfs track "*.psd"
$ git add .gitattributes
$ git add file.psd
$ git commit -m "Add design file"
$ git push origin main
常用 Git LFS 命令
# 追踪指定的大文件,支持特定文件名,也支持正则表达式
git lfs track 'test.txt'
# 查看现有的文件追踪模式
git lfs track
# 取消某文件的追踪
git lfs untrack 'test.txt'
# 查看git lfs 对象的状态
git lfs status
# 显示当前跟踪的文件列表
git lfs ls-files
# 检查当前所用lfs的版本
git lfs version
# 迁移已有的仓库
git lfs migrate import --include="*.psd" --everything
# 已有的仓库不适合 git lfs,则可以恢复
git lfs migrate export --include="*.psd" --everything
# 设置自己的 LFS 服务器 API 接口
git config -f .lfsconfig lfs.url https://my_other_server.example.com/foo/bar/info/lfs git add .lfsconfig
# 只获取仓库本身,不获取任何LFS对象
GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/xx/xx.git
# 获取当前commit下包含的LFS对象的当前版本
git lfs pull
# 仅获取指定目录下的LFS对象
git config lfs.fetchinclude 'images/**' #配置指定包含某文件夹
git config lfs.fetchexclude 'videos/**' #配置排除
# 一次获取LFS对象的最近版本
# Git LFS 相关命令在获取 LFS 对象时,默认仅会获取该对象当前被引用的版本,如果想要一次获取 LFS 对象的当前及最近版本的话,首先需要对最近进行定义
git config lfs.fetchrecentcommitsdays 7 # 7表示同时下载过去7天内的版本该项配置默认值为 0,即不获取过去的版本,而仅获取指定的版本。
# 有了对最近的定义后,我们可以选择在执行 git lfs fetch 命令时,加上 --recent 参数以同时获取最近版本或者配置
git config lfs.fetchrecentalways true
效果
执行 GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/xx/xx.git 命令后,得到的无二进制文件项目所占磁盘存储与原始项目对比如下,可以看到节省了一些磁盘存储。
xiaobaiyang@ubuntu2004server:~/projects/discovery$ du
4 ./.git/branches
668 ./.git/objects/pack
4 ./.git/objects/info
1072 ./.git/objects
16 ./.git/logs/refs/remotes/origin
20 ./.git/logs/refs/remotes
12 ./.git/logs/refs/heads
36 ./.git/logs/refs
44 ./.git/logs
8 ./.git/info
16 ./.git/refs/remotes/origin
20 ./.git/refs/remotes
12 ./.git/refs/heads
4 ./.git/refs/tags
40 ./.git/refs
80 ./.git/hooks
4 ./.git/lfs/tmp
548 ./.git/lfs/objects/ec/de
552 ./.git/lfs/objects/ec
368 ./.git/lfs/objects/f4/98
372 ./.git/lfs/objects/f4
9652 ./.git/lfs/objects
9692 ./.git/lfs
10976 ./.git
548 ./discovery/statics/pics
1312 ./discovery/statics/css/bootstrap-3.3.7-dist/css
120 ./discovery/statics/css/bootstrap-3.3.7-dist/js
224 ./discovery/statics/css/bootstrap-3.3.7-dist/fonts
1660 ./discovery/statics/css/bootstrap-3.3.7-dist
2028 ./discovery/statics/css
2580 ./discovery/statics
16 ./discovery/templates/base
24 ./discovery/templates
24 ./discovery/discovery
2636 ./discovery
13636 .
|
xiaobaiyang@ubuntu2004server:~/projects/test/discovery/discovery$ du
4 ./.git/branches
676 ./.git/objects/pack
4 ./.git/objects/info
684 ./.git/objects
8 ./.git/logs/refs/remotes/origin
12 ./.git/logs/refs/remotes
8 ./.git/logs/refs/heads
24 ./.git/logs/refs
32 ./.git/logs
8 ./.git/info
8 ./.git/refs/remotes/origin
12 ./.git/refs/remotes
8 ./.git/refs/heads
4 ./.git/refs/tags
28 ./.git/refs
80 ./.git/hooks
4 ./.git/lfs/tmp
4 ./.git/lfs/objects/ec/de
8 ./.git/lfs/objects/ec
4 ./.git/lfs/objects/f4/98
8 ./.git/lfs/objects/f4
20 ./.git/lfs/objects
28 ./.git/lfs
888 ./.git
8 ./discovery/statics/pics
1312 ./discovery/statics/css/bootstrap-3.3.7-dist/css
120 ./discovery/statics/css/bootstrap-3.3.7-dist/js
224 ./discovery/statics/css/bootstrap-3.3.7-dist/fonts
1660 ./discovery/statics/css/bootstrap-3.3.7-dist
1668 ./discovery/statics/css
1680 ./discovery/statics
16 ./discovery/templates/base
24 ./discovery/templates
24 ./discovery/discovery
1736 ./discovery
2648 .
|
参考资料
https://github.com/git-lfs/git-lfs
https://github.com/git-lfs/git-lfs/blob/main/lfs/hook.go
https://sabicalija.github.io/git-lfs-intro/
https://www.cnblogs.com/dylanchu/p/13970667.html
https://github.com/sinbad/lfs-folderstore
https://blog.csdn.net/weixin_43862847/article/details/121374089