$ cat .git/hooks/pre-commit
#!/bin/bash
#
# This command prevent commit if total file size exceeds error is occured.
#
if [ -z "$filesizelimit" ]; then
#### TODO: custom change
#filesizelimit=1000 # 1M
filesizelimit=400 # 400K
fi
total=0
for fname in `git diff-index --cached HEAD --name-only`; do
if [ -f $fname ]; then
size=`ls -Al $fname | awk '{ print $5 }'`
total=`expr $total + $size`
fi
done
filesize=`expr $total / 1024`
if [ $filesize -gt $filesizelimit ]; then
filename=`git diff-index -z --cached HEAD --name-only`
echo "# =================" >&2
echo "Error: Too large commit attempted." >&2
echo >&2
echo "File's size limit is $filesizelimit KB" >&2
echo "File's named $filename of size $filesize KB." >&2
exit 1
fi
exit 0
$ git commit -a
# =================
Error: Too large commit attempted.
File's size limit is 400 KB
File's named 1.png2.png of size 443 KB.