Introduction
Large files in Git repositories slow down clones, fetches, and everyday operations. This guide covers LFS implementation and repository optimization techniques.
Symptoms
- Git operations extremely slow
- Repository size much larger than working directory
- Long clone times for new developers
- Memory issues during git commands
Step-by-Step Fix
- 1.Identify large files in history:
- 2.```bash
- 3.git rev-list --objects --all |
- 4.git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
- 5.sed -n 's/^blob //p' |
- 6.sort --numeric-sort --key=2 |
- 7.tail -n 20
- 8.
` - 9.Set up Git LFS:
- 10.```bash
- 11.git lfs install
- 12.git lfs track "*.psd"
- 13.git lfs track "*.zip"
- 14.git add .gitattributes
- 15.git commit -m "Add LFS tracking"
- 16.
` - 17.Use partial clones for large repos:
- 18.```bash
- 19.git clone --filter=blob:none --no-checkout https://github.com/org/repo.git
- 20.git sparse-checkout init --cone
- 21.git sparse-checkout set src/ tests/
- 22.
`