将大文件添加到 Git LFS (Large File Storage)

Git LFS 是一个 Git 扩展,用于更有效地处理大文件。以下是将大文件添加到 Git LFS 的步骤:

1. 安装 Git LFS

首先,你需要安装 Git LFS:

bash 复制代码
# 在 macOS 上 (使用 Homebrew)

brew install git-lfs

# 在 Ubuntu/Debian 上

apt-get install git-lfs

# 在 Windows 上 (使用 Chocolatey)

choco install git-lfs

# 使用 Git LFS 安装程序

# 从 https://git-lfs.github.com/ 下载并安装

2. 在你的仓库中设置 Git LFS

在你的 Git 仓库中初始化 LFS:

bash 复制代码
cd your-repository

git lfs install

3. 指定要通过 LFS 追踪的文件类型

使用 git lfs track 命令指定要通过 LFS 管理的文件类型:

bash 复制代码
# 跟踪特定扩展名

git lfs track "*.psd"

git lfs track "*.zip"

git lfs track "*.pdf"

# 跟踪特定文件

git lfs track "path/to/large-file.bin"

# 跟踪特定目录中的所有文件

git lfs track "large-directory/**"

这些命令会更新 .gitattributes 文件,你需要确保将此文件添加到 Git 中:

bash 复制代码
git add .gitattributes

4. 添加和提交大文件

现在你可以正常添加和提交文件,被跟踪的大文件将自动通过 LFS 处理:

bash 复制代码
git add large-file.psd

git commit -m "Add large design file"

5. 将现有的大文件迁移到 LFS

如果你已经在 Git 仓库中有大文件,想要将它们迁移到 LFS:

bash 复制代码
# 首先设置跟踪

git lfs track "*.large-extension"

# 然后使用 git lfs migrate

git lfs migrate import --include="*.large-extension" --everything

6. 推送到远程仓库

推送到远程仓库时,LFS 文件将被自动处理:

bash 复制代码
git push origin main

7. 检查 LFS 跟踪的文件

查看当前仓库中 LFS 正在跟踪的文件类型:

bash 复制代码
git lfs track

查看已被 LFS 管理的文件:

bash 复制代码
git lfs ls-files

注意事项

  1. 确保远程仓库支持 Git LFS(GitHub、GitLab、Bitbucket 等主流平台都支持)
  1. 大多数 Git 托管服务对 LFS 存储有容量限制或额外收费
  1. 团队中的所有开发者需要安装 Git LFS
  1. .gitattributes 文件应当被提交到仓库中
  1. 迁移现有文件到 LFS 会重写历史,需谨慎操作,特别是在共享仓库中
相关推荐
特级业务专家2 小时前
这下发布不需要Jenkins了
linux·git·docker
0和1的舞者2 小时前
Git 实战踩坑:如何让多个 IDE 项目共用一个远程仓库(附子模块问题解决)
git·开发·仓库·码云·子模块·操作·冲突
妖孽白YoonA4 小时前
Git Worktree:同时处理多个分支的正确姿势
git
-拟墨画扇-4 小时前
Git | 配置别名操作
git·gitee·github·gitcode
-拟墨画扇-5 小时前
Git | 多人协助策略
git·gitee·github·gitcode
BestOrNothing_20155 小时前
Git 原理彻底讲透:工作区 / 暂存区 / 本地仓库 / origin 一次理清(VSCode 可视化 + 实战避坑)
git·分支开发·fetch / merge·add / commit·pull / push·工作区/暂存区/本地仓库
Huazzi.5 小时前
使用Scoop安装Git
git·elasticsearch·gitee·ssh·github·scoop
马达加斯加D6 小时前
git --- reset 删除提交
git
10000hours6 小时前
【Git】git常用命令
git
KevinRay_6 小时前
Github如何上传项目(超详细小白教程)
git·gitee·开源·github·vim