git lfs installation and push, pull large files into GitHub

Git LFS Installation and Git LFS Push, Pull – Simple Guide

Last Updated on April 19, 2024 by cscontents

Introduction

Git LFS (Large File Storage) – a revolutionary extension to Git that empowers developers to seamlessly manage large files with precision and ease. From multimedia assets to datasets and beyond, Git LFS provides a scalable solution for storing, versioning, and collaborating on files of any size.

As the size of files within repositories continues to grow, traditional version control systems face limitations in efficiently handling large files. In those cases, we can use Git LFS to manage large files.

Important points to be noted:

  • Using a browser, we can upload a file of size 25 MiB (maximum) or less.
  • Using the command line, we can upload a file of size of 100 MiB (maximum) or less.
  • If the file size is more than 100 MiB, then we need to use Git LFS (Large File Storage).

The main point is, that GitHub has a limitation for large files in any repository. To track those large files beyond a certain size limit we need to use Git Large File Storage (LFS).

How does Git LFS work?

Unlike Git’s traditional approach of storing entire files, Git LFS leverages a referencing system. Instead of storing the actual large file in GitHub, Git LFS creates a pointer file within the repository. This pointer acts as a map, containing information about the location of the large file, typically stored in a separate storage solution.

Below is a table that shows the size limits of large files that can be tracked using Git LFS (depending on the GitHub Plan).

Plan Maximum file size (in GB) Comments
GitHub Free 2 GB It means using the GitHub free plan, any file can’t be larger than 2 GB. Otherwise, we won’t be able to track, or version control that file.
GitHub Pro 2 GB Maximum file size can be 2 GB which can be tracked.
GitHub Team 4 GB Maximum file size can be 4 GB which can be tracked.
GitHub Enterprise Cloud 5 GB Maximum file size can be 5 GB which can be tracked.

Git LFS installation on Linux

We will go through the steps for Git LFS installation on Linux (Ubuntu, RHEL, and CentOS).

Prerequisite

First, Before installing Git LFS we need to make sure Git is installed on our machine. On Linux Git version 1.8.2 or higher is required for Git LFS.

Check the git version by running the below command.

git --version

Second, you need to have a non-root user created in the machine with proper sudo privileges to perform the installation.

Git LFS installation on Ubuntu

We can install Git LFS in two ways –

  • Using package manager – Here we will add the packagecloud repository and do the installation using the Ubuntu package manager “apt”.
  • Using Git LFS Binaries (archive installation) – Here we will download the Git LFS binary files and then do the installation.
Using Package Manager

Follow the below steps to install Git LFS using the package manager (apt).

Step 1: Add the packagecloud repository

We will be using packagecloud (a platform that is used for managing software packages). This packagecloud hosts the git-lfs packages.

Execute the below command.

curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
Step 2: Install the Git LFS package

Run the below command to install Git LFS.

sudo apt-get install git-lfs -y
Step 3: Validate the installation

Run the below command.

git-lfs --version
Using Git LFS Binaries (archive installation)

Here we will see how to install Git LFS by downloading the binary files.

We will install git-lfs version 3.3.0, you can install any other version as per your requirement.

Please check the below URL for various releases of Git LFS and you can easily download the binaries of the required version.

https://github.com/git-lfs/git-lfs/releases

Step 1: Download the Git LFS Binary files

Run the below command to download the binary file using wget.

wget https://github.com/git-lfs/git-lfs/releases/download/v3.3.0/git-lfs-linux-amd64-v3.3.0.tar.gz
Step 2: Uncompress the downloaded file

Run the below command to uncompress the downloaded .tar.gz file.

tar -xvf git-lfs-linux-amd64-v3.3.0.tar.gz

After execution of the above command, we will see a folder created with the name git-lfs-3.3.0

Step 3: Install Git LFS

In this step, we will install Git LFS. In the previous step, we saw a new directory created after uncompressing the downloaded tar file. We need to go inside that directory.

cd git-lfs-3.3.0/

Next, run the below command, it will execute a script.

  • This script installs Git LFS in the system (can be verified from the $PATH variable after installation).
  • It runs the “git lfs install” command to perform the global configuration changes.
sudo ./install.sh

After execution of the above shell script (install.sh file), Git LFS will be initialized.

Step 4: Validate the Git LFS installation

Run the below command to validate the installation and it should show the version of the installed Git LFS.

git-lfs --version

Git LFS installation on RHEL / CentOS

We can install Git LFS in two ways –

  • Using package manager – Here we will add the packagecloud repository and do the installation using the RHEL / CentOS package manager “yum”.
  • Using Git LFS Binaries (archive installation) – Here we will download the Git LFS binary files and then do the installation.
Using Package Manager YUM

Follow the below steps to install Git LFS using the package manager (yum).

Step 1: Add the packagecloud repository

We will be using packagecloud (a platform that is used for managing software packages). This packagecloud hosts the git-lfs packages.

Execute the below command.

curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.rpm.sh | sudo bash 
Step 2: Install the Git LFS package

Run the below command to install Git LFS.

sudo yum install git-lfs -y
Step 3: Validate the installation

Run the below command.

git-lfs --version
Using Git LFS Binaries (archive installation)

Here we will see how to install Git LFS by downloading the binary files.

We will install git-lfs version 3.3.0, you can install any other version as per your requirement.

Please check the below URL for various releases of Git LFS and you can easily download the binaries of the required version.

https://github.com/git-lfs/git-lfs/releases

Step 1: Download the Git LFS Binary file

Run the below command to download the binary file.

wget https://github.com/git-lfs/git-lfs/releases/download/v3.3.0/git-lfs-linux-amd64-v3.3.0.tar.gz
Step 2: Uncompress the downloaded file

Run the below command to uncompress the downloaded .tar.gz file.

tar -xvf git-lfs-linux-amd64-v3.3.0.tar.gz

After execution of the above command, we will see a folder created with the name git-lfs-3.3.0

Step 3: Install Git LFS

In this step, we will install Git LFS. In the previous step, we saw a new directory created after uncompressing the downloaded tar file. We need to go inside that directory.

cd git-lfs-3.3.0/

Inside this git-lfs-3.3.0/ directory, there will be a shell script. This script installs Git LFS in the system (can be verified from $PATH variable after installation) and it runs the “git lfs install” command to perform the global configuration changes.

Now, run the below command.

sudo ./install.sh

After execution of the above shell script (install.sh file), Git LFS will be initialized.

Step 4: Validate the Git LFS installation

Run the below command to validate the installation and it should show the version of the installed Git LFS.

git-lfs --version

Git LFS installation on Windows

For Windows, Git LFS is included in the distribution of Git. It means, that if we install Git on any Windows machine, Git LFS will be automatically installed on that machine.

To verify, run the below commands.

git --version

and

git-lfs --version

Output:

C:\Users>git --version
git version 2.23.0.windows.1
C:\Users>git-lfs --version
git-lfs/2.8.0 (GitHub; windows amd64; go 1.12.2; git 30af66bb)

How to do Git LFS push?

Here we will see how to do git-lfs push. If you have large files in your local repository and you want to push them into GitHub, in that case, follow the below steps to do so.

Step 1: Track the large files

At first, we need to track the large files using Git LFS. Otherwise, if we try to go “git push” it will fail. For example, we have a large tar file with the name test_file.tar.gz

Run the below command to track the large file.

git lfs track test_file.tar.gz

Running the above command will create a new file with the name “.gitattributes”. This .gitattributes file will have all the details about large files that we want to push using Git LFS.

Step 2: Run Normal Git Commands

Once we tracked the larger files, now we need to execute the normal git commands for pushing the files into GitHub.

Add the changes,

git add .

Commit the changes into the local repository,

git commit -m “Adding test_file.tar.gz file”

Push the changes into GitHub (the below command will push into the master branch),

git push origin master

This is all about pushing large files (>100 MiB) into GitHub using Git LFS.

How to do Git LFS pull?

When we have any large file (Git LFS tracked file) in GitHub, then we can easily pull them. Below are few points regarding the same.

  • When there is any Git LFS tracked file in GitHub there will be a .gitattributes file in the repository.
  • If we have already cloned the repository on a local machine and then a large file is placed into GitHub. In that case, we can execute the “git lfs pull” command.
  • Also, if there are Git LFS tracked files in GitHub and if you run normal “git clone” to clone the repository then it will automatically trigger the pulling of the git lfs tracked file.
  • We can also run the “git lfs clone” command to clone any git repo that has Git LFS tracked files. Though this command is depreciated.

For more detailed information, you can follow the official site of Git LFS: https://git-lfs.com/

 

Thank you.

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.