Installing and Mastering Git LFS: Manage Large Binary Files Without Repo Bloat

Git tutorial - IT technology blog
Git tutorial - IT technology blog

Why Does Standard Git Struggle with Large Files?

Imagine joining a Unity game project, an AI/ML initiative, or a web application packed with 4K demo videos. You run git clone and wait impatiently for 30 minutes just to pull down a 15GB directory. That experience is a genuine nightmare.

The root cause lies in Git’s storage architecture. Git was engineered for text-based source code and tracks entire commit histories. Whenever you modify a few frames in a 100MB intro.mp4 video and commit the changes, Git creates a brand-new blob object weighing nearly 100MB inside the .git directory. After 10 edits, your repository accumulates roughly 1GB of dead data. Consequently, clone, fetch, and push speeds grind to a crawl. GitHub will even outright reject pushes containing any single file exceeding 100MB.

Git LFS (Large File Storage) solves this problem using a clever mechanism: pointer files.

  • The actual binary file is pushed to dedicated storage, such as a designated LFS server or an S3 bucket.
  • Both local and remote Git repositories only store a tiny pointer text file (around 130 bytes). This file records the SHA-256 hash along with the exact binary size.
  • Whenever you switch branches using git checkout, Git LFS hooks instantly download the matching binary version into your working directory.

Installing Git LFS Across Operating Systems

Git LFS is an official open-source extension for Git. Start by installing the appropriate binary for your system.

1. On macOS

The quickest way is via Homebrew:

brew install git-lfs

2. On Ubuntu / Debian

sudo apt update
sudo apt install git-lfs

3. On Windows

The Git for Windows installer comes with Git LFS bundled by default. If you use a package manager, simply run:

choco install git-lfs
# or
scoop install git-lfs

4. Initializing Global Hooks

Once the binary is installed, run this command once:

git lfs install

This command registers filter drivers (clean, smudge, process) into your ~/.gitconfig file. Git will then automatically detect and enable LFS across all repositories on your machine.

Configuration Setup and Standard Workflow

With installation complete, you need to specify which file patterns Git LFS should manage.

1. Tracking File Patterns

Navigate to your project directory and declare your binary patterns, such as graphics, videos, or AI model weights:

cd my-awesome-project

# Track by file extension (use quotes to prevent shell expansion)
git lfs track "*.psd"
git lfs track "*.mp4"
git lfs track "*.onnx"

# Or track all files within a model directory
git lfs track "models/**"

This command creates or updates rules inside your .gitattributes file—the core of LFS configuration. Be sure to commit this file to Git before adding any binary files:

git add .gitattributes
git commit -m "build: configure git lfs tracking patterns"

2. Daily Workflow: Add, Commit, and Push

Git LFS operates seamlessly in the background. You interact with familiar Git commands without needing to adopt an entirely new workflow:

# Copy a 180MB design file into the project
cp ~/Downloads/banner_hero.psd assets/

git add assets/banner_hero.psd
git commit -m "feat(assets): add hero banner for landing page"
git push origin main

During the push, the terminal displays two distinct stages: Git LFS uploads the binary chunks to the LFS storage server first, and Git then pushes the commit metadata and pointer files to the remote repository.

3. Preventing Merge Disasters with File Locking

Binary files cannot be merged automatically line by line. In practice, if two designers edit a 200MB Photoshop file concurrently on different branches, one of them will inevitably lose their work. Git LFS remedies this via File Locking:

  • Mark file types as lockable during tracking: git lfs track "*.psd" --lockable
  • Acquire an exclusive lock before opening a file to edit: git lfs lock assets/banner_hero.psd
  • Team members running git lfs locks will see the file is locked and cannot push conflicting changes.
  • Once your edits are pushed, release the lock for your teammates: git lfs unlock assets/banner_hero.psd

Production Optimization and Troubleshooting Tips

Managing LFS properly keeps your project running smoothly and prevents unexpected cloud storage bills.

1. Verifying Files Tracked by LFS

To ensure a binary file wasn’t accidentally committed as a raw blob in the Git tree, verify with:

git lfs ls-files

If the terminal prints the file path along with a SHA-256 hash, your configuration is working properly. If the output is empty, the file was committed directly into Git’s internal database.

2. Inspecting Pointer File Contents

Curious about what Git actually stores in place of that 180MB file? Run:

git show HEAD:assets/banner_hero.psd

The output consists of just three plain text lines:

version https://git-lfs.github.com/spec/v1
oid sha256:7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
size 188743680

3. Accelerating CI/CD by Skipping LFS Downloads

On CI/CD runners (GitHub Actions, GitLab CI), linting or unit test steps usually only require source code. Downloading tens of gigabytes of models or video assets wastes bandwidth quota and stretches build times from 1 minute to 15 minutes. Configure your clone to bypass LFS payload downloads:

# Skip downloading actual binary payloads during clone
GIT_LFS_SKIP_SMUDGE=1 git clone [email protected]:my-team/core-app.git

# Only pull binaries when test steps strictly require them:
git lfs pull --include="assets/critical-fixtures/**"

4. Reclaiming Disk Space with Prune

After months of branching and switching, the local .git/lfs/objects directory can swell to 40-50GB of outdated binary versions. Safely free up local disk space with:

git lfs prune

This command only purges local LFS files no longer referenced by your current commit or active branches, keeping your repository history completely intact.

Share: