如何将 git 跟踪的文件转换为 git-lfs?

How to convert a file tracked by git to git-lfs?

如何将已作为常规对象提交的单个文件(例如 png)转换为 git-lfs?

我不想迁移整个仓库,也不想重写仓库的历史。

git lfs track 将开始跟踪新文件 已签入您的存储库的现有文件。当您 运行 git lfs track 然后提交该更改时,它将更新文件,将其替换为 LFS 指针内容。

这里我有一个存储库,其中签入了 PNG "normally"(不使用 LFS):

C:\Temp\LFS>dir
 Volume in drive C is Windows
 Volume Serial Number is A442-238A

 Directory of C:\Temp\LFS

11/09/2017  11:12 AM    <DIR>          .
11/09/2017  11:12 AM    <DIR>          ..
10/20/2017  01:22 PM            48,517 VSTS.png
               1 File(s)         48,517 bytes
               2 Dir(s)  284,988,436,480 bytes free

C:\Temp\LFS>git status
On branch master
nothing to commit, working tree clean

我不需要对 PNG 进行任何更改,我可以简单地 git lfs track 它:

C:\Temp\LFS>git lfs track VSTS.png
Tracking "VSTS.png"    

并且 git-lfs 已经完成了它需要做的一切 - 它现在在 .gitattributes 中设置了跟踪信息 git知道VSTS.png修改为:

C:\Temp\LFS>git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   VSTS.png

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitattributes

no changes added to commit (use "git add" and/or "git commit -a")

现在如果我们 git add 文件,它会将 LFS 对象添加到存储库,我们可以提交更改。 实际上 要做的是 git-lfs 将该文件排入 LFS 存储队列,并用指针文件替换存储库中的文件,告诉 LFS 在哪里可以找到实际的 blob ,如果我们检查存储库阶段的内容,我们可以看到:

C:\Temp\LFS>git add VSTS.png

C:\Temp\LFS>git cat-file blob :0:VSTS.png
version https://git-lfs.github.com/spec/v1
oid sha256:6075cd5130bdbe29a037cba93dc22158d1443b22b2ffb6acb0d1d541838f26b9
size 48517

(这是实际的 LFS 元数据,而不是 PNG 文件。)

现在,当您提交并推送这些更改时,该文件将位于 LFS 中。然而,这不具有追溯力 - 您没有重写存储库的历史记录,因此以前版本的 PNG 仍将直接位于存储库中(而不是在 LFS 中)并且仍将占用 space.

如果你确实想重写历史,我建议使用 BFG Repo Cleaner