Introduction

GitHub is a commonly used code management tool, and it is also dubbed the world's largest programmer dating site. Every upgrade of it will affect many users. On my personal github website, I uploaded a large self-made file when I was doing the JAVA NIO demo. I have made some changes to this project recently, but it cannot be uploaded to the github website. Check the reason for the error, that is, the project There is a large file that cannot be uploaded, and now github provides a tool called Git Large File Storage (LFS) to replace the large file in github.

So what is LFS and how should we use LFS? Let's take a look together.

LFS and its installation

The full name of LFS is Git Large File Storage, which can store large files in the library on remote servers such as GitHub.com or GitHub Enterprise, and store links to these large files in the library.

LFS is relatively simple to install. On a mac, you can use the following brew command:

brew install git-lfs

After installation, you need to associate LFS with your git account:

git lfs install
Note lfs requires git version >= 1.8.2

Then we can happily use LFS.

Use of LFS

In order to simulate large files on github, we can create a new repository on github, and then execute the following command to add the corresponding content:

git init .
echo Hello World > README.md
git add README.md
git commit -m "Initial commit"

There is definitely no problem with submitting the above code to github.

In order to test large files, we can use the dd command to create a large file of 256M as follows:

dd if=/dev/urandom of=test.bin bs=1024 count=262144

In the latest version of github, this file must not be uploaded, so how should we use LFS?

lfs provides the following help commands:

$ git lfs help <command>
$ git lfs <command> -h

Here we need to use the git lfs track command as follows:

git lfs track '*.bin'

In the above example we are using wildcards to match all files ending with bin.

Of course, if you want to match the test.bin file above, you can also use it directly like this:

git lfs track 'test.bin'

The purpose of this command is to use lfs to track these bin files. You can also use the track command to list all tracked paths by lfs:

git lfs track

Listing tracked paths
    *.bin (.gitattributes)

We can use the following command to view the specific trace files of lsf:

git lfs ls-files

But because you haven't committed the bin file created above, you can't see it here.

The trace information of these lfs is stored in .gitattributes in the project root directory.

We need to commit this .gitattributes file together so that all users who checkout this repository can be aware of this lfs service.

When we have submitted all the files, and then use the git lfs ls-files command, we can see something similar to the following:

d05241dd24b * test.bin

Indicates that this file has been added to lfs.

Delete files from LFS

Above we explained how to add files to LFS for tracking, what if we don't want to use LFS but use traditional git to manage files?

lfs provides the untrack command, which is the opposite of track as follows:

git lfs untrack "*file-type"

After untracking, also remember to delete this file from the git cache:

git rm --cached "*file-type"

Then re-add this file to git, commit and submit:

git add "*file-type"
git commit -m "restore "*file-type" to git from lfs"

Pull code from LFS

Pulling code from LFS is the same as pulling code from traditional ordinary git, just use the git clone command directly:

git clone https://github.com/username/test_repo.git destination_dir

Convert historical data to LFS

Sometimes our files are already in the repository, such as many historically submitted files, what if we want to convert them to LFS storage?

lfs provides a migrate command to dump files.

git lfs migrate

To use the migrate command, you need to install Git LFS v2.2.1 or later.

For example, if we want to migrate all bin files, we can write:

git lfs migrate import --include="*.bin"

The above command is only for local branches. If you want to migrate remote branches, you can add a --include-ref parameter, as shown below:

git lfs migrate import --include="*.bin" --include-ref=refs/heads/master 

Then force committing the library:

git push --force

Finally, although the above command converts historical git objects into lfs objects, there is no change in the local .git file, so you need to execute the following command to clean up the .git folder:

git reflog expire --expire-unreachable=now --all
git gc --prune=now

Summarize

If you are not using large files, then you will not use lfs. If you have large files in your project, then refer to this article.

This article has been included in http://www.flydean.com/07-git-largefile/

The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tricks you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!


flydean
890 声望437 粉丝

欢迎访问我的个人网站:www.flydean.com