An easy way to prevent large GitHub checkins

If you’re using GitHub as a remote repo, you may have run into the problem of it failing on files on larger than 100MB. If you run into this limitation, then at least I have to dig through a bunch of webpages I’ve searched before to try and remember how to rewrite history to get rid of my commits of large files. If it’s the last commit, it’s easy, a git reset HEAD~1 will get rid of it, but if you have to rewrite your git history, then life can get really painful, especially if it happens just frequently enough to annoy you, but not often enough that you memorize what you did. Anyway, I finally got fed up with it, and started looking for a solution. All the ones I found were exceedingly complicated, and required either ruby, python, or some other scripting language. This is fine, when you’re on a Mac or a Linux box, but for those of us either running Windows or who want to try and keep their machine clean of un-needed software, it’s a pain.
I finally got fed up, and wrote a script to fix this issue, that has absolutely no dependencies. It will also get a list of ALL the files that are too large, and print them out in a way that can be cut and pasted directly into your .gitignore file. I spent the time writing it, so hopefully it helps someone else out too. Here’s my script, which you can copy directly into .git/hooks/pre-commit on your machine. It’s not elegant, but works on any platform, with no dependencies, using just git’s shell and commands.

if git rev-parse --verify HEAD >/dev/null 2>&1
    # Initial commit: diff against an empty tree object

# Redirect output to stderr.
exec 1>&2

maximumsize=100000000 #Error if we are over 100MB
rm -f ./error_files_transport_temp
git diff --cached --name-status --diff-filter=ACM | while read st file; do
    # skip deleted files
    if [ "$st" == 'D' ]; then continue; fi
    filesize=$(wc -c <"$file") 
    if [ $filesize -ge $maximumsize ]; then echo $file >> ./error_files_transport_temp; fi
if [ -e ./error_files_transport_temp ]; then
    echo One or more files are greater than 100MB!
    echo -------------------------------------
    cat ./error_files_transport_temp
    rm -f ./error_files_transport_temp
    exit 1