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

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

本文介绍了如何将git跟踪的文件转换为git-lfs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I convert a single file (e.g. a png) which is already committed as a regular object to git-lfs?

我不想迁移整个存储库,也不想重写存储库的历史记录.

I do not want to migrate the whole repo and I also do not want to rewrite the history of the repo.

推荐答案

git lfs track将开始跟踪新文件已经签入到存储库中的现有文件.当您运行git lfs track并提交更改时,它将更新文件,并用LFS指针内容替换它.

git lfs track will begin tracking a new file or an existing file that is already checked in to your repository. When you run git lfs track and then commit that change, it will update the file, replacing it with the LFS pointer contents.

在这里,我有一个存储库,其中的PNG已正常"检入(不使用LFS):

Here I have a repository with a PNG checked in "normally" (without using 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即可:

I don't need to make any changes to the PNG, I can simply git lfs track it:

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

并且git-lfs已经完成了它需要做的所有事情-它在.gitattributes中设置了跟踪信息,现在git知道VSTS.png已被修改:

And git-lfs has done everything it needs to - it's set up the tracking information in the .gitattributes, and now git knows that VSTS.png is modified:

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,我们可以看到如果我们检查存储库阶段的内容:

Now if we git add the file, it will add the LFS object to the repository, and we can commit the changes. What will actually be done is that git-lfs will enqueue that file for LFS storage, and replace the file in the repository with a pointer file, telling LFS where to find the actual blob, which we can see if we inspect the contents of the repository's stage:

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文件.)

(That's the actual LFS metadata, instead of the PNG file.)

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

Now when you commit and push these changes, the file will be in LFS. This is not retroactive however - you are not rewriting the repository's history, so previous versions of the PNG will still be in the repository directly (not in LFS) and will still be taking up space.

如果您确实想重写历史记录,我建议您使用 BFG回购清洁器.

If you did want to rewrite the history, I would recommend using the BFG Repo Cleaner.

这篇关于如何将git跟踪的文件转换为git-lfs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 16:18