GitHub documentation on large files states:
If you attempt to add or update a file that is larger than 50 MB, you will receive a warning from Git.
GitHub blocks pushes that exceed 100 MB.
I want to configure it in a way that it blocks any pushes with files larger than some predefined threshold (e.g. 100kb).
Is this possible? How?
Maybe via the branch protection rules? I saw some examples that the branch protection rules can include custom status checks. The status checks, these are GitHub actions? So I could maybe add a custom GitHub CI action which checks for a valid commit (valid = any changed file is below the size limit).
Maybe it's not really possible yet. I also asked in the official community forum.
(Note: If possible, I would prefer a pure-Git answer, independent of GitHub. With pure Git, this is possible via a server-side pre-receive hook. See here. However, on GitHub, I cannot have a custom pre-receive hook, so this is not an option.)
In the meantime, you can. Go to your repository's settings -> rules -> rulesets -> new ruleset -> new push ruleset. There, you can select Restrict file size and configure a limit in MB.
Most of the time I try to commit a large file, it's an error, so I use a pre-commit hook. If you don't have any hooks setup, you can just create the file /.git/hooks/pre-commit and put the following bash in it
#!/bin/bash
max_file_size=10000000
for filename in $(git diff --cached --name-only --diff-filter=A HEAD); do
filesize=$(stat -c%s "$filename")
if (( filesize > max_file_size )) ; then
echo "The file "$filename" is more than $max_file_size bytes. Git commit cancled." >/dev/stderr
exit -1
fi
done
Then make the hook executable with chmod +x /.git/hooks/pre-commit. This script will block files with more than 10000000 bytes. Some people get fancy with their pre-commits using them to run all sorts of tests and code quality checks, using tools like the pre-commit framework.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With