Binaries, vendored tarballs, an accidental node_modules, that 40 MB
training CSV someone swore they'd .gitignore — they all get committed
the same way: by accident, by someone in a hurry, and nobody notices
until the clone takes four minutes and the history is poisoned forever.
Git history is forever. The cheapest place to stop a fat commit is before it happens. That's all this is: a pre-commit hook that weighs your staged changes and refuses the commit if they're too heavy.
There are two flavors. Pick the one that matches how strict you want to be.
git-diff-size-check.rb is the opinionated one. No single file may
grow by more than 1 MiB, and the whole commit may not exceed 4 MiB. Break
either rule and it tells you exactly which file blew the budget, then
walks away. Use this when you want a firm hand.
git-diff-size-check-total-only.rb only cares about the bottom line.
Stay under 4 MB in total and it doesn't care how you got there. Use this
when a single large-but-legitimate file is fine, but a commit full of
them isn't.
The scripts are executable and self-contained — Ruby and git, nothing else. Drop one in as your pre-commit hook:
ln -s ../../git-diff-size-check.rb .git/hooks/pre-commit
That's it. Now every git commit gets weighed first. Reject the commit,
trim the fat, commit again. The size limits are constants at the top of
each file; if 4 MB is the wrong number for you, it's the wrong number for
about ten seconds, because that's how long it takes to change it.
The original idea and code are Joe Miller's — credit where it's due, that's the gist this one was forked from. What changed here:
- It diffs the staged index against
HEAD, the way a pre-commit hook is supposed to, instead of comparing the working tree to a branch calledmasterthat your repo may not even have. - Renames and copies no longer crash it. The old code only knew
A,M, andD; anything else handed it aniland the whole thing fell over. Now every status is accounted for. - The very first commit in a fresh repo works, because there's no
HEADyet and the script knows to fall back to the empty tree instead of exploding.
Same spirit, fewer sharp edges.