[Git FAQs] How to Enable Git LFS for a Repository and Track Large Files

How to Enable Git LFS for a Repository and Track Large Files

Git Large File Storage (LFS) is an extension for Git that allows efficient handling of large binary files by storing them outside the main Git repository while keeping lightweight pointers in place. This helps prevent repository bloat and improves performance when working with large files.

In this guide, we’ll cover:

  1. How to enable Git LFS in a repository
  2. How to track common large file types using Git LFS

1. Enabling Git LFS in a Repository

Before you start tracking large files, you need to initialize Git LFS in your repository. Follow these steps:

Step 1: Install Git LFS (if not already installed)

If Git LFS is not installed, install it using the following command:

For macOS (Homebrew)

1
brew install git-lfs

For Ubuntu/Debian

1
sudo apt install git-lfs

For Windows (via Chocolatey)

1
choco install git-lfs

Step 2: Initialize Git LFS in Your Repository

Navigate to your Git repository and run:

1
git lfs install

This initializes Git LFS for your system. If you want to make it default for all repositories, use:

1
git lfs install --global

2. Tracking Large File Types in Git LFS

Once Git LFS is installed and initialized, you can start tracking large files. Below are some common file types that should be stored in Git LFS instead of the main Git repository.

Step 3: Add Common Large File Types to Git LFS

Run the following commands to track different types of large files:

1️⃣ Images

1
git lfs track "*.png" "*.jpg" "*.jpeg" "*.gif" "*.svg"

2️⃣ Videos

1
git lfs track "*.mp4" "*.avi" "*.mov" "*.mkv"

3️⃣ Audio Files

1
git lfs track "*.mp3" "*.wav" "*.flac"

4️⃣ Archives & Compressed Files

1
git lfs track "*.zip" "*.tar" "*.gz" "*.rar" "*.7z" "*.bz2" "*.iso"

5️⃣ Executables & Installers

1
git lfs track "*.exe" "*.dll" "*.dmg" "*.apk" "*.ipa"

6️⃣ Machine Learning & Data Files

1
git lfs track "*.h5" "*.hdf5" "*.pth" "*.pb" "*.onnx" "*.npy" "*.npz"

7️⃣ Office Documents (Word, Excel, PowerPoint, PDF)

1
2
3
4
git lfs track "*.doc" "*.docx" "*.odt"
git lfs track "*.xls" "*.xlsx" "*.ods" "*.csv"
git lfs track "*.ppt" "*.pptx" "*.odp"
git lfs track "*.pdf"

8️⃣ Design & 3D Files

1
2
git lfs track "*.psd" "*.ai" "*.sketch" "*.blend"
git lfs track "*.fbx" "*.obj"

9️⃣ Game Development & Fonts

1
2
git lfs track "*.unitypackage" "*.pak" "*.uasset" "*.umap"
git lfs track "*.ttf" "*.otf"

Step 4: Commit and Push Changes

Once tracking rules are added, Git LFS updates a .gitattributes file in your repository. Commit and push the changes:

1
2
3
git add .gitattributes
git commit -m "Add Git LFS tracking for large files"
git push origin main

Step 5: Verify Git LFS Tracking

To confirm that Git LFS is tracking the files, run:

1
git lfs ls-files

This command lists all files currently tracked by Git LFS.

3. Handling Existing Large Files in a Git Repository

If large files were already committed before enabling Git LFS, you should remove them from Git history and re-add them using LFS.

Step 6: Remove Previously Committed Large Files

1
2
3
4
5
6
git rm --cached <large-file>
git commit -m "Remove large file from repository"
git lfs track "<large-file>"
git add <large-file>
git commit -m "Re-add file to LFS"
git push origin main

If multiple large files were previously committed, use the Git LFS migrate tool:

1
git lfs migrate import --include="*.mp4,*.zip,*.exe"

4. Prevent Unwanted Files from Being Tracked

To prevent unnecessary files (like macOS system files) from being tracked in Git, add them to .gitignore.

Step 7: Ignore Unwanted Files

1
2
3
4
echo ".DS_Store" >> .gitignore
git add .gitignore
git commit -m "Ignore .DS_Store files"
git push origin main

This ensures that .DS_Store and similar system files are never included in commits.

5. Summary & Best Practices

File Type Should Use LFS? Example Extensions
Images ✅ Yes .png, .jpg, .gif
Videos ✅ Yes .mp4, .avi, .mov
Audio ✅ Yes .mp3, .wav, .flac
Archives ✅ Yes .zip, .tar, .rar
Executables ✅ Yes .exe, .dmg, .apk
ML/Data Files ✅ Yes .h5, .onnx, .npy
Office Docs ✅ Yes .docx, .xlsx, .pptx
Design/3D Files ✅ Yes .psd, .ai, .blend
Game Assets ✅ Yes .uasset, .unitypackage
Source Code ❌ No .py, .js, .java

Key Takeaways

✔️ Use git lfs install to enable LFS.
✔️ Use git lfs track "<pattern>" to specify which files to track.
✔️ Commit and push .gitattributes to apply LFS settings across the team.
✔️ Use git lfs migrate import to retroactively move large files to LFS.
✔️ Add .gitignore rules to exclude unnecessary files like .DS_Store.

By setting up Git LFS properly, you can keep your repository lightweight, improve performance, and avoid unnecessary bloating caused by large files. 🚀

Happy coding! 🎯

References

[1] Git LFS Official Documentation - https://git-lfs.github.com

[2] GitHub Docs: Managing Large Files - https://docs.github.com/en/github/managing-large-files

[3] Git LFS Tutorial by Atlassian - https://www.atlassian.com/git/tutorials/git-lfs

[4] Git LFS Cheat Sheet - https://www.git-tower.com/learn/git/cheat-sheet/git-lfs

[5] Git LFS FAQ - https://github.com/git-lfs/git-lfs/wiki/Tutorial

[6] How to Migrate Existing Repositories to Git LFS - https://www.gitkraken.com/learn/git/git-lfs

[7] Using Git LFS with Bitbucket - https://support.atlassian.com/bitbucket-cloud/docs/use-git-lfs-with-bitbucket/

[8] Git LFS Commands List - https://github.com/git-lfs/git-lfs/blob/main/docs/man/git-lfs.md