What is Git LFS?
Git is not designed to handle large binary files well. When you commit large files like audio, video, or datasets, the repository size grows quickly. This makes cloning and fetching slow for everyone.
Git LFS (Large File Storage) is a Git extension that solves this problem. It replaces large files with small text pointers in your Git repository, while storing the actual file contents on a separate, remote server.
Problem Scenario
You need to add a 200MB video file to your project. When you try to push the commit, you might see a warning or an error from your Git host (like GitHub) about file size limits. Even if it succeeds, the push is very slow, and anyone cloning the repository will have to download this large file.
Solution
1. Install Git LFS
First, you need to install the Git LFS client on your local machine.
- On macOS: Use Homebrew:
brew install git-lfs
- On Windows: Use Chocolatey:
choco install git-lfs
or download from the official website. - On Linux: Use your package manager, e.g.,
sudo apt-get install git-lfs
on Debian/Ubuntu.
After installation, run this command once per user account:
git lfs install
2. Track File Types with LFS
In your repository, you need to tell Git LFS which files to track. This is done using the git lfs track
command. Itโs best to use patterns rather than individual filenames.
Letโs say we want to track all .mp4
video files.
git lfs track "*.mp4"
This command creates or updates a .gitattributes
file in your repository. This file contains the tracking information.
*.mp4 filter=lfs diff=lfs merge=lfs -text
3. Commit .gitattributes
The .gitattributes
file must be committed to your repository so that other collaborators use the same LFS tracking rules.
git add .gitattributes
git commit -m "Configure Git LFS for video files"
4. Add and Commit Your Large File
Now, you can add your large file to the repository as you normally would.
git add my-large-video.mp4
git commit -m "Add large video file"
5. Push to the Remote
When you push, Git LFS intercepts the process. It uploads the large file to the LFS server and pushes the small pointer file to the Git repository.
git push origin main
You will see output indicating that the LFS file is being uploaded.
How it Works for Others
When another user clones the repository, they will get the pointer files. Git LFS will then automatically download the actual large files from the LFS server. If they donโt have Git LFS installed, they will only have the pointer files and will see an error if they try to use them.
Conclusion
Git LFS is the standard way to handle large files in Git. It keeps your repository small and fast while allowing you to version large assets alongside your code. Remember to configure which file types to track in .gitattributes
before you add the large files to your repository.
Leave a comment