How to Track Large Files in Git LFS Easily
To track large files in
git lfs, first install Git LFS, then run git lfs track "" to specify which files to track. Finally, commit the changes to .gitattributes and push your files as usual.Syntax
The basic command to track large files with Git LFS is git lfs track ". This tells Git LFS to manage files matching the pattern.
git lfs track: Command to start tracking files with Git LFS.": The file name or pattern (like" *.psd) to track.- After tracking, Git LFS updates the
.gitattributesfile to record these patterns.
Remember to commit the .gitattributes file so others also track the same files.
bash
git lfs track "<file-pattern>"Example
This example shows how to track a large image file named photo.png using Git LFS.
bash
git lfs install git lfs track "photo.png" git add .gitattributes photo.png git commit -m "Track photo.png with Git LFS" git push origin main
Output
Git LFS initialized.
Tracking "photo.png"
[main abc1234] Track photo.png with Git LFS
2 files changed, 2 insertions(+)
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1.23 MiB | 1.23 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To origin/main
Common Pitfalls
Common mistakes when using Git LFS include:
- Not running
git lfs installbefore tracking files, which sets up Git LFS hooks. - Forgetting to commit the
.gitattributesfile, so others won't track the files properly. - Trying to track files after they are already committed without rewriting history, which won't retroactively convert them.
- Using incorrect file patterns that don't match the intended files.
Always track files before adding them to Git.
bash
## Wrong way (tracking after commit) git add largefile.zip # Commit without tracking git commit -m "Add largefile.zip" # Then track (too late) git lfs track "largefile.zip" # .gitattributes updated but largefile.zip is already in Git history ## Right way git lfs track "largefile.zip" git add .gitattributes largefile.zip git commit -m "Track largefile.zip with Git LFS"
Quick Reference
Summary tips for tracking large files with Git LFS:
- Run
git lfs installonce per machine. - Use
git lfs track "before adding files." - Commit the
.gitattributesfile to share tracking rules. - Push files normally; Git LFS handles large file storage.
- Check tracked files with
git lfs ls-files.
Key Takeaways
Always run 'git lfs install' before tracking large files to set up Git LFS.
Use 'git lfs track ""' before adding files to Git to enable LFS tracking.
Commit the '.gitattributes' file so tracking rules apply for everyone.
Track files before committing; tracking after commit won't convert existing files.
Use 'git lfs ls-files' to verify which files are tracked by Git LFS.