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. 1.Identify large files in history:
  2. 2.```bash
  3. 3.git rev-list --objects --all |
  4. 4.git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
  5. 5.sed -n 's/^blob //p' |
  6. 6.sort --numeric-sort --key=2 |
  7. 7.tail -n 20
  8. 8.`
  9. 9.Set up Git LFS:
  10. 10.```bash
  11. 11.git lfs install
  12. 12.git lfs track "*.psd"
  13. 13.git lfs track "*.zip"
  14. 14.git add .gitattributes
  15. 15.git commit -m "Add LFS tracking"
  16. 16.`
  17. 17.Use partial clones for large repos:
  18. 18.```bash
  19. 19.git clone --filter=blob:none --no-checkout https://github.com/org/repo.git
  20. 20.git sparse-checkout init --cone
  21. 21.git sparse-checkout set src/ tests/
  22. 22.`