Introducing Git LFS

Tram Ho

What is Git LFS?

Git is a distributed version control system, which means the entire history of the repo is passed on to the client during the cloning process. For projects containing large files, especially those that are frequently edited on a regular basis, the initial cloning step can take time, because each version has to be downloaded by the user. Git LFS (Large File Storage) is a Git utility developed by Github, Atlassian, and a few other open source contributors. It reduces the impact of large files in your repo by downloading the relevant version of that file. In particular, large files are downloaded during checkout rather than during cloning or fetching.

Git LFS does this by replacing large files in your repo with a small pointer. During use, you will never see pointer files because they are processed automatically using Git LFS:

  1. When you add a file to your repo, Git LFS replaces the file’s contents with a pointer, and stores the file’s contents in the Git LFS cache.

  1. When you push a new commit to the server, any Git LFS files referenced by the commit just pushed will be moved from the local Git LFS cache to the Git LFS store associated with your repo.

  1. When you checkout a commit that contains Git LFS cursors, they will be replaced with files from your Git LFS cache or from the Git LFS store.

Git LFS is very flexible: when you work you only see the contents of the file. That means you can use Git LFS without altering the existing Git flow in the project. You simply git add git commit as usual. git clone , git pull will be significantly faster because you only download the version of the file referenced by the commit.

How to use Git LFS

Setting

  1. There are 3 easy ways to install Git LFS:
  • Install it through package managers.
  • Download and install slowly from the home page
  • Install through 3rd party
  1. Once git-lfs is installed on your machine, run git lfs install to initiate Git LFS.

You only need to run git lfs install once. Once initialized, Git LFS starts automatically every time you clone a repo containing Git LFS content.

Create a new Git LFS repo

To create a new Git LFS repo, you need to run git lfs install after creating the repo:

Clone 1 Git LFS repo

Once the Git LFS is installed you can clone the Git LFS repo as you normally would using the git clone at the end of the clone Git will checkout the default branch, usually master, and any Git LFS files will be loaded automatically. about to complete the clone process for you.

There are 4 PNG files in this repo followed by Git LFS. When you clone, Git LFS will download those files once

Clone acceleration

If you are cloning 1 repo with lots of Git LFS files, git lfs clone will produce much better performance:

Don’t download Git LFS files once at a time, git lfs clone will wait when the whole checkout is finished and download the Git LFS files as a batch. This will take advantage of the parallel download capabilities, and significantly reduce the number of HTTP requests and processes generated.

Pull and checkout

Like the clone process, you can pull from the Git LFS repo using git pull . Any necessary Git LFS files will be downloaded as part of the automated checkout process

Accelerated pull

Similar to git lfs clone , git lfs pull will download Git LFS files as a batch.

Fetch lots of Git LFS history

Git LFS actually only downloads the files needed for the commit. But you can force Git LFS to download more content for recently changed branches using git lfs –recent.

This is useful when you batch download Git LFS content while you are busy. Git LFS will give branches or tags that contain commits over the last few days as recent . This number can also be reconfigured by changing the lfs.fetchrecentrefsdays setting

By default, git lfs fetch --recent will only download Git LFS commit assets at the beginning of recent branches or recent tags.

However, you can also configure Git LFS to download the contents of the commits earlier.

You can also download full Git LFS content using git lfs fetch --all

Delete local Git LFS files

You can delete Git LFS files in Git LFS cache locally using git lfs prune .

It will remove all the supposed old Git LFS files. An old file is one that is not referenced by:

  • Commit is being checked out
  • Commit has not been pushed
  • Commit recently

By default, the recent commit is the commit created within the last 10 days. This value is equal to the sum of:

  • Value of lfs.fetchrecentrefsdays (default is 7)
  • Value lfs.pruneoffsetdays (default is 3)

You can also configure for a longer period:

Unlike the garbage collection system available in Git, Git LFS content is not deleted automatically, so running git lfs prune is a basic idea to keep code size down.

Limiting the Git LFS file

In some situations you want to download only a sub-file of Git LFS assets for a particular commit. You can use the following command

or just certain files

or a combination of the two

Conclude

Git LFS is a great extension for working with Git and large files. Hope this article has helped you to understand the basics of Git LFS, how it works as well as a basic guide. See you again.

Refer

https://medium.com/swlh/learning-about-git-large-file-system-lfs-72e0c86cfbaf https://medium.com/junior-dev/how-to-use-git-lfs-large-file -storage-to-push-large-files-to-github-41c8db1e2d65 https://www.atlassian.com/git/tutorials/git-lfs

Share the news now

Source : Viblo