Installing and Configuring Git LFS

Git Large File Storage (Git LFS), a tool used to manage large files and binaries in Git repositories. Standard Git wasn’t designed to handle large files, and Git LFS provides a solution by storing large files outside the repository while keeping lightweight pointers within it.

Introduction to Git LFS

Git Large File Storage (Git LFS) is a powerful extension for Git that allows efficient tracking of large files. Instead of storing entire file versions in the Git repository, Git LFS stores only references (pointers) to large files, which are saved separately on a designated server. When cloning, Git LFS ensures only the necessary versions of the large files are retrieved, optimizing the size and speed of Git operations.

Example Use Cases:

  • Large binary files, such as .zip archives or .exe files
  • High-resolution images and graphics
  • Audio or video files
  • Machine learning datasets or models

Benefits of Using Git LFS

Git LFS improves the workflow in various ways:

  • Performance: Git commands remain fast and responsive as large files aren’t stored directly.
  • Storage Efficiency: Large files are stored once and referenced with pointers, preventing repository size bloat.
  • Collaborative Ease: Cloning and pulling from the repository become faster as Git LFS downloads only required files.
  • Version Control for Large Files: You can version and update large files as needed without duplicating content.

Installing Git LFS

The first step to using Git LFS is to install it on your local machine.

Step 1: Installing Git LFS

Installation varies slightly based on the operating system. Here are the most common installation methods:

  • macOS:

				
					brew install git-lfs

				
			
  • Linux:
				
					sudo apt-get install git-lfs

				
			
  • Windows: Download Git for Windows, which includes Git LFS, or install it via the Git LFS website.

Step 2: Initializing Git LFS

After installation, enable Git LFS for your repository with:

				
					git lfs install

				
			

Output: Running git lfs install enables Git LFS hooks for the repository, which will manage large files automatically.

Configuring Git LFS for Your Repository

After installing Git LFS, it’s time to configure it to track specific types of files in your repository. This configuration is done by adding tracking rules to a .gitattributes file, which controls which file types Git LFS will manage.

Tracking Large Files

To configure Git LFS to track a certain type of file, use the git lfs track command. For example, to track .png images:

				
					git lfs track "*.png"

				
			

Explanation:

  • *.png: Specifies all .png files, instructing Git LFS to manage these files instead of Git.

After adding a file type for tracking, Git LFS automatically updates the .gitattributes file with an entry.

				
					*.png filter=lfs diff=lfs merge=lfs -text

				
			

Committing the .gitattributes File

Once Git LFS is configured to track specific files, commit the .gitattributes file to the repository.

				
					git add .gitattributes
git commit -m "Configure Git LFS to track .png files"

				
			

Tracking Files with Git LFS

You can add specific files to Git LFS tracking in addition to file types.

Example of Tracking an Individual File

To track a specific file (e.g., large-file.zip):

				
					git lfs track "large-file.zip"
git add .gitattributes
git commit -m "Track large-file.zip with Git LFS"


				
			

Working with Git LFS Tracked Files

Git LFS tracks large files automatically once configured, allowing you to add, commit, push, pull, and clone repositories without manually managing large files.

Adding and Committing Files

Once a file is tracked by Git LFS, commit and push it to the repository like any other file:

				
					git add large-image.png
git commit -m "Add large image to repository"

				
			

Output: When you check the commit history, you’ll see a pointer reference for large-image.png rather than the full file content.

Advanced Configurations

Git LFS includes advanced options, including setting a custom storage location, managing quotas, and more.

Custom Git LFS Server

If your organization has a custom server for large file storage, configure Git LFS to use it:

				
					git config lfs.url <custom-server-url>

				
			

Explanation: This command redirects Git LFS to use a specific server for file storage, rather than the default provider.

Managing Storage Quotas

Git LFS offers commands to monitor and manage storage usage, helping you stay within limits:

				
					git lfs ls-files

				
			

Output: Lists all files managed by Git LFS along with their sizes, giving you insight into storage consumption.

Example Workflow with Git LFS

Below is an example workflow for integrating Git LFS into a project.

Step-by-Step Example

1. Initialize Git LFS:

				
					git lfs install

				
			

2. Track Large File Types:

				
					git lfs track "*.jpg"
git lfs track "*.mp4"

				
			

3. Commit the Tracking Configuration:

				
					git add .gitattributes
git commit -m "Add .jpg and .mp4 files to Git LFS tracking"


				
			

4. Add and Commit Large Files:

				
					git add large-video.mp4
git commit -m "Add large video file"
				
			

5. Push to Remote:

				
					git push origin main

				
			

Output: This workflow demonstrates a typical setup where specific file types are tracked and managed using Git LFS, allowing large media files to be added without performance issues.

Git LFS is essential for managing large files in Git, enabling efficient storage, retrieval, and management of files that are traditionally problematic for Git. By using Git LFS, developers can track large files seamlessly, allowing for a smoother Git workflow and efficient team collaboration. Happy Coding!❤️

Table of Contents