Lar*_*lke 4 git version-control git-lfs
如何将已经作为常规对象提交的单个文件(例如png)转换为git-lfs?
我不想迁移整个存储库,也不想重写存储库的历史记录。
Edw*_*son 10
git lfs track
将开始跟踪已签入到存储库中的新文件或现有文件。当您运行git lfs track
然后提交更改时,它将更新文件,并用LFS指针内容替换它。
在这里,我有一个带有“正常”签入的PNG的存储库(不使用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
Run Code Online (Sandbox Code Playgroud)
我不需要对PNG进行任何更改,我可以简单地git lfs track
做到这一点:
C:\Temp\LFS>git lfs track VSTS.png
Tracking "VSTS.png"
Run Code Online (Sandbox Code Playgroud)
和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")
Run Code Online (Sandbox Code Playgroud)
现在,如果我们git add
添加了文件,它将把LFS对象添加到存储库中,我们可以提交更改。会有什么实际做的是,git-lfs
将排队等待LFS存储该文件,并用指针文件替换资源库中的文件,告诉LFS在哪里可以找到实际的斑点,我们可以看到,如果我们检查库的阶段的内容:
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
Run Code Online (Sandbox Code Playgroud)
(这是实际的LFS元数据,而不是PNG文件。)
现在,当您提交并推送这些更改时,该文件将位于LFS中。但是,这不是追溯性的-您不会重写存储库的历史记录,因此以前的PNG版本仍将直接位于存储库中(不在LFS中),并且仍会占用空间。
如果您确实想重写历史记录,我建议您使用BFG Repo Cleaner。
根据git-lfs 教程:
只是跟踪文件不会将它们转换为 lfs。它们已经是历史的一部分,转换旧文件的唯一方法是重写历史 [...]
相反,在启用 LFS 跟踪时需要“重新添加”文件。因此,首先启用跟踪:
git lfs track VSTS.png
git add .gitattributes
git commit -m "Enable LFS tracking for VSTS.png"
Run Code Online (Sandbox Code Playgroud)
然后在启用 LFS 跟踪的同时重新添加文件(具有相同的内容):
git rm --cached VSTS.png
git add VSTS.png
git commit -m "Re-add VSTS.png as an LFS object"
Run Code Online (Sandbox Code Playgroud)
当然 git 历史记录仍然包括全部内容,VSTS.png
因为它最初是在没有 LFS 跟踪的情况下提交的。