Open In App

Git – LFS (Large File Storage)

Improve
Improve
Like Article
Like
Save
Share
Report

Git-LFS replaces the large files in the repository with tiny pointers. It means if we have some large files in our repository like any Images, Videos, Graphics, etc then cloning that repository is a time and space-consuming process. The developer may not need all those large resource files but still, he needs to download them while cloning the repository. 

Git – LFS solves this problem. It allows the developers to avoid downloading the large resource files and provides some tiny pointers in the cloned repository. Those pointers point towards the actual file located at the remote servers like GitHub or GitLab, etc. In case the developer checkout to that particular branch where there is a requirement of such big files then at that moment only the files get downloaded from the server. Git – LFS uses Git Hooks to perform this operation.

Let’s understand in a practical way how we can set up and use the Git LFS.

In order to use Git LFS, first, we need to install it on our system. We can use the following command to do so.

sudo apt-get install git-lfs

Here is the terminal shell pictorial depiction after executing the above commands as follows:  

Now on the repository where we want to use Git-LFS, there we have to set up that.

Step 1: Run the command on the repository. 

git lfs install 

It will initialize the LFS in the repository and will update the Git Hooks.

Step 2: Run the below command as listed:

git lfs track "*.jpg" 

This will tell git lfs to handle the jpg files, in case you want any other file, then you can specify that particular extension.

Step 3: As the configuration that is done on Step 2, is stored in the .gitattributes file, so we will add it for commit using the following command

git add .gitattributes 

Here is the terminal shell pictorial depiction after executing the above commands as follows:  

Step 4: After we have done this, we will create a new branch and add all the large files there and push the new branch into the remote repository.

Tip: If you don’t know how to set up a remote branch and push code to it, then go through Git tutorial 

Step 5: Now let us clone the repository from the remote.

Step 6: After cloning if we look at the size of the cloned repository and the original repository we can see that there is a big difference between their sizes.

The original Repository is as follows: 

Cloned Repository is as follows: 

So, here we can see that the original repository has a size of 9.2 MB but the cloned repository is only 172KB as the Git-LFS only put tiny pointers of those large image files and not the actual images on the cloned repository.

Step 7: Now if we check out to that branch where the large files are present, then at that moment only the actual files will be downloaded from the remote servers.

Here is the terminal shell pictorial depiction after executing the above commands as follows:  

As we can see from the screenshot after we checkout to the large_file_branch then at that moment the actual files are getting downloaded.


Last Updated : 11 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads