Video in progress

Strong Middle 🌿 Git

Big binary files (test dumps, images, model files) were added to the repository and a clone has grown to 3 GB. Why is this a problem, and how do you fix it?

Git stores every version in full, and every clone downloads the whole history, while binaries delta-compress badly. For new files you use Git LFS, and to clean the old history you need git filter-repo and a force push.

📖 Answer

Deleting does not help: if you git rm a file, it stays in the old commits and is still downloaded with every clone.

First, find what is taking the space:

git rev-list --objects --all \
  | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
  | awk '$1=="blob"' | sort -k3 -n -r | head -20

For the future, Git LFS. Only a small pointer file stays in the repository, and the real content lives on the LFS server:

git lfs install
git lfs track "*.psd" "src/test/resources/dumps/*.sql.gz"
git add .gitattributes

Cleaning the old history means rewriting history:

git filter-repo --path src/test/resources/dumps --invert-paths
# or move the existing ones to LFS:
git lfs migrate import --include="*.sql.gz" --everything
git push --force --all && git push --force --tags

This changes all commit hashes: everyone has to clone again and open PRs have to be reopened. That is why I do it in a "maintenance window" announced in advance.

In many cases the best solution is to take binaries out of Git entirely: test data is generated with Testcontainers, artifacts live in Nexus/Artifactory, and big files sit in S3.