Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Git & GitHub - Git LFS

Using Git Large File Storage (LFS)

Git Large File Storage (LFS) is an extension for managing large files in Git repositories. It replaces large files with text pointers inside Git, while storing the file contents on a remote server. This guide covers how to use Git LFS to handle large files efficiently.

Key Points:

  • Git LFS replaces large files in your repository with small pointer files.
  • Large file contents are stored on a remote server, reducing the size of your local repository.
  • Git LFS is useful for managing binary files such as images, videos, and datasets.

Setting Up Git LFS

Step 1: Install Git LFS

Install Git LFS on your system:


# Install Git LFS (example for macOS using Homebrew)
$ brew install git-lfs

# Install Git LFS (example for Ubuntu)
$ sudo apt-get install git-lfs

# Install Git LFS (example for Windows)
$ choco install git-lfs
                

Step 2: Initialize Git LFS

Initialize Git LFS in your repository:


# Initialize Git LFS
$ git lfs install
                

Tracking Large Files

Step 1: Track File Types

Specify the file types to be tracked by Git LFS:


# Track image files (e.g., PNG, JPG)
$ git lfs track "*.png"
$ git lfs track "*.jpg"

# Track other large file types (e.g., PSD, MP4)
$ git lfs track "*.psd"
$ git lfs track "*.mp4"

# Add the .gitattributes file to the repository
$ git add .gitattributes
$ git commit -m "Track large files with Git LFS"
                

Step 2: Add and Commit Files

Add and commit the large files to your repository:


# Add and commit large files
$ git add largefile.png largefile.mp4
$ git commit -m "Add large files using Git LFS"
                

Pushing and Pulling with Git LFS

Pushing Files

Push the changes to your remote repository. Git LFS will handle the large files:


# Push changes to the remote repository
$ git push origin main
                

Pulling Files

When you pull changes from the remote repository, Git LFS will download the large file contents:


# Pull changes from the remote repository
$ git pull origin main
                

Managing LFS Files

Viewing Tracked Files

List the file types being tracked by Git LFS:


# List tracked file types
$ git lfs track
                

Untracking Files

Remove files from being tracked by Git LFS:


# Untrack a file type
$ git lfs untrack "*.mp4"

# Commit the changes to .gitattributes
$ git add .gitattributes
$ git commit -m "Untrack MP4 files with Git LFS"
                

Cleaning Up LFS Files

Clean up LFS files that are no longer referenced:


# Clean up LFS files
$ git lfs prune
                

Best Practices

Follow these best practices when using Git LFS:

  • Track Only Necessary Files: Use Git LFS to track only the file types that need it, such as large binary files.
  • Regularly Clean Up LFS Files: Use git lfs prune to remove unreferenced LFS files and save space.
  • Communicate with Your Team: Ensure all team members have Git LFS installed and understand how to use it.
  • Monitor Repository Size: Keep an eye on your repository size and LFS storage usage to manage costs and performance.

Summary

This guide covered how to use Git Large File Storage (LFS) to manage large files in Git repositories. By using Git LFS, you can efficiently handle large files, keep your repository size manageable, and ensure smooth collaboration within your team.