🔀 One Accidental 400MB Video File Commit – and It’s in History Forever
Someone on the team accidentally commits a large binary file – a video, a database dump, a build artifact – straight into the repository. It gets caught in review and reverted in a later commit, but the repository stays bloated forever, because Git never actually forgets a blob just because a later commit stops referencing it in the working tree.
🐞 The Problem
$ git add . $ git commit -m "Add demo video" $ du -sh .git 1.2G .git <- was 40MB before this commit # Even after deleting the file in a LATER commit, # the 400MB blob stays in every clone's history forever.
🔍 Why This Happens
Git tracks the FULL HISTORY of every blob, not just the current state of the working tree. Deleting a file in a new commit just means the CURRENT tree no longer references that blob - the blob itself, and every commit that ever pointed at it, stays in .git permanently, and every future clone has to download it, forever, even though nobody looking at the current code will ever see it.
✅ The Fix: Block Large Files Before They’re Committed
- Add a pre-commit hook that checks staged file sizes and refuses the commit if anything exceeds a threshold (e.g. 5MB), with a clear message pointing to the right place for large assets (Git LFS, cloud storage, a CDN).
- Install it as a SHARED hook via a tool like Husky or a repo-level
core.hooksPathsetting committed to the repo, so it applies for every teammate automatically rather than depending on each person setting it up manually. - For assets that genuinely need to live alongside the code (design files, sample media), use Git LFS specifically – it stores the large content outside the main object history while still versioning it properly.
📦 Simple Pre-Commit Size Check
#!/bin/sh
# .git/hooks/pre-commit (or via Husky)
MAX_SIZE=5242880 # 5MB in bytes
for file in $(git diff --cached --name-only); do
size=$(git cat-file -s :"$file" 2>/dev/null || echo 0)
if [ "$size" -gt "$MAX_SIZE" ]; then
echo "❌ $file is $(($size / 1048576))MB - exceeds 5MB limit."
echo " Use Git LFS or external storage instead."
exit 1
fi
done
A commit is forever by design — the only real fix for an oversized file in history is preventing the commit in the first place, not cleaning up after it lands.
