如何缩小 git LFS 仓库

How to shrink a git LFS repo

在我公司,我们用git LFS 存储系统开发相关的文件。此存储库包含 CAD 数据、图像、文档(*.pdf、*.docx、*.xlsx)等。绝对有必要将所有文件置于版本控制之下。然而,存储库的使用方式最终导致存储库的总大小达到 8GB,尽管所有文件都被 LFS 跟踪。

基本上所有东西都被推送到 master 分支上,这似乎让 LFS 存储过时了,因为没有任何东西可以被修剪以节省本地存储。

问题

有没有办法让更多的本地对象被追溯修剪?

您可以使用以下命令

git lfs prune

来自 docs,

DESCRIPTION

Deletes local copies of LFS files which are old, thus freeing up disk space. Prune operates by enumerating all the locally stored objects, and then deleting any which are not referenced by at least ONE of the following:

  • the current checkout
  • a 'recent branch'; see [RECENT FILES]
  • a 'recent commit' on the current branch or recent branches; see [RECENT FILES]
  • a commit which has not been pushed; see [UNPUSHED LFS FILES]
  • any other worktree checkouts; see git-worktree(1)

In general terms, prune will delete files you're not currently using and which are not 'recent', so long as they've been pushed i.e. the local copy is not the only one.

The reflog is not considered, only commits. Therefore LFS objects that are only referenced by orphaned commits are always deleted.

Note: you should not run git lfs prune if you have different repositories sharing the same custom storage directory; see git-lfs-config(1) for more details about lfs.storage option.


由于当前签出正在引用 LFS 跟踪文件,因此您无法修剪,您可以使用 GIT_LFS_SKIP_SMUDGE setting[=21 克隆没有 LFS 跟踪文件的本地存储库=]

export GIT_LFS_SKIP_SMUDGE=1
git clone /path/to/local/repo test

这应该会减少存储库的大小,因为 LFS 跟踪文件将被转换为指针文件。如果你想使用 LFS 跟踪文件你需要做一个 git lfs pull

由于 git lfs pull 将下载当前结帐中的所有 LFS 跟踪文件,您可以使用包含和排除 options 来包含或排除特定文件。

例如,您可以通过

在存储库中包含所有 pdf(LFS 跟踪文件)
git lfs pull -I "*.pdf"

或通过

排除它们
git lfs pull -X "*.pdf"

请注意,您可以从远程仓库本身克隆,将本地仓库克隆到测试仓库只是一个示例,表明大小已减小。