I'm fully aware of this question which technically makes this a duplicate but the answers only offer a solution on push, which would be too late for my requirements.
Is there an option to limit the file size when committing?
For example: file sizes above 500K would produce a warning. File sizes above 10M would stop the commit.
There is a general pre-commit hook. You can write a script to check file size and then accept or reject the commit. Git however gives the user the ability to bypass the check) from the command line type "git help hooks" for more information. Here is the relevant info on the pre-commit hook.
pre-commit
This hook is invoked by git commit, and can be bypassed with --no-verify option. It takes no parameter, and is invoked before obtaining the proposed commit log message and making a commit. Exiting with non-zero status from this script causes the git commit to abort.
You need to implement eis script you already look for in the
pre-commit
hook.From documentation, we learned that pre-commit hook
Basically, the hook is called to check if the user is allowed to commit his changes.
The script originally made by eis on other post becomes
A shorter, bash-specific version of @Leon's script, which prints the file sizes in a human-readable format. It requires a newer git for the
--diff-filter=d
option:As with the other answers, this must be saved with execute permissions as
.git/hooks/pre-commit
.Example output:
This pre-commit hook will do the file size check:
.git/hooks/pre-commit
Above script must be saved as
.git/hooks/pre-commit
with execution permissions enabled (chmod +x .git/hooks/pre-commit
).The default soft (warning) and hard (error) size limits are set to 500,000 and 10,000,000 bytes but can be overriden through the
hooks.filesizesoftlimit
andhooks.filesizehardlimit
settings respectively: