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:
- 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.
- 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.
- 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
- 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
- Once git-lfs is installed on your machine, run git lfs install to initiate Git LFS.
1 2 | $ git lfs install Git LFS initialized. |
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:
1 2 | # initialize Git $ mkdir Atlasteroids $ cd Atlasteroids $ git init Initialized empty Git repository in /Users/tpettersen/Atlasteroids/.git/ # initialize Git LFS $ git lfs install Updated pre-push hook. Git LFS initialized. |
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.
1 2 | $ git clone <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> :tpettersen/Atlasteroids.git Cloning into 'Atlasteroids'... remote: Counting objects: 156, done. remote: Compressing objects: 100% (154/154), done. remote: Total 156 (delta 87), reused 0 (delta 0) Receiving objects: 100% (156/156), 54.04 KiB | 31.00 KiB/s, done. Resolving deltas: 100% (87/87), done. Checking connectivity... done. Downloading Assets/Sprites/projectiles-spritesheet.png (21.14 KB) Downloading Assets/Sprites/productlogos_cmyk-spritesheet.png (301.96 KB) Downloading Assets/Sprites/shuttle2.png (1.62 KB) Downloading Assets/Sprites/space1.png (1.11 MB) Checking out files: 100% (81/81), done. |
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:
1 2 | $ git lfs clone <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> :tpettersen/Atlasteroids.git Cloning into 'Atlasteroids'... remote: Counting objects: 156, done. remote: Compressing objects: 100% (154/154), done. remote: Total 156 (delta 87), reused 0 (delta 0) Receiving objects: 100% (156/156), 54.04 KiB | 0 bytes/s, done. Resolving deltas: 100% (87/87), done. Checking connectivity... done. Git LFS: (4 of 4 files) 1.14 MB / 1.15 MB |
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
1 2 | $ git pull Updating 4784e9d..7039f0a Downloading Assets/Sprites/powerup.png (21.14 KB) Fast-forward Assets/Sprites/powerup.png | 3 + Assets/Sprites/powerup.png.meta | 4133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 4136 insertions(+) create mode 100644 Assets/Sprites/projectiles-spritesheet.png create mode 100644 Assets/Sprites/projectiles-spritesheet.png.meta |
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.
1 2 | $ git lfs fetch --recent Fetching master Git LFS: (0 of 0 files, 14 skipped) 0 B / 0 B, 2.83 MB skipped Fetching recent branches within 7 days Fetching origin/power-ups Git LFS: (8 of 8 files, 4 skipped) 408.42 KB / 408.42 KB, 2.81 MB skipped Fetching origin/more-music Git LFS: (1 of 1 files, 14 skipped) 1.68 MB / 1.68 MB, 2.83 MB skipped |
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
1 2 | # download Git LFS content for branches or tags updated in the last 10 days $ git config lfs.fetchrecentrefsdays 10 |
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.
1 2 | # download the latest 3 days of Git LFS content for each recent branch or tag $ git config lfs.fetchrecentcommitsdays 3 |
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:
1 2 | # don't prune commits younger than four weeks (7 + 21) $ git config lfs.pruneoffsetdays 21 |
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
1 2 | $ git lfs fetch -X "Assets/**" |
or just certain files
1 2 | $ git lfs fetch -I "*.ogg,*.wav" |
or a combination of the two
1 2 | $ git lfs fetch -I "Assets/**" -X "*.gif" |
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