Git Hooks for Automation
What you'll build
Three executable hooks in .git/hooks/ that enforce a commit message convention, block files over 1MB, and run tests before push.
Goals
- Write a pre-commit hook as a shell script
- Enforce a commit message format with commit-msg
- Run tests automatically with pre-push
- Make hooks executable and test them
Prerequisites
- Basic shell scripting
- Git commit and push workflow
- File permissions (chmod +x)
Steps
- 1
Write a pre-commit hook
Create .git/hooks/pre-commit that checks for files larger than 1MB in the staged changes. If found, print an error and exit 1 (which blocks the commit).
Code#!/bin/sh # .git/hooks/pre-commit max_size=1048576 # 1MB for file in $(git diff --cached --name-only); do if [ -f "$file" ]; then size=$(wc -c < "$file") if [ $size -gt $max_size ]; then echo "Error: $file is $(($size / 1024))KB, exceeds 1MB limit" exit 1 fi fi done exit 0Expected result
Committing a 2MB file fails with the error message; small files commit normally.
- 2
Make the hook executable
Git only runs hooks that are executable. Use chmod +x to set the permission, then test by committing a large file.
Codechmod +x .git/hooks/pre-commit # Test: dd if=/dev/zero of=bigfile.bin bs=1M count=2 git add bigfile.bin git commit -m 'Add big file' # Should be blockedExpected result
The commit is blocked with the size error.
- 3
Write a commit-msg hook
Create .git/hooks/commit-msg that enforces a 'type: message' format (e.g. 'feat: add login'). Read the message from $1, check with grep, exit 1 if invalid.
Hint
The hook receives the path to the temporary commit message file as $1. Use grep -E to match the pattern.
- 4
Write a pre-push hook
Create .git/hooks/pre-push that runs your test suite. If tests fail, the push is blocked. Read stdin for ref updates (you can ignore it for simple hooks).
Hint
Run npm test or make test and check the exit code. exit 1 blocks the push.
- 5
Test all hooks end-to-end
Test each hook: try a bad commit message (blocked), a large file (blocked), and a failing test (push blocked). Then verify valid commits and pushes succeed.
Code# Test commit-msg: git commit -m 'bad message' # blocked git commit -m 'feat: valid message' # succeeds # Test pre-push: # (with a failing test in the suite) git push # blocked if tests failExpected result
Invalid commits and pushes are blocked; valid ones succeed.
Try it yourself
#!/bin/sh
# .git/hooks/pre-commit
# TODO: check for large files#!/bin/sh
for f in $(git diff --cached --name-only); do [ -f "$f" ] && [ $(wc -c < "$f") -gt 1048576 ] && echo "$f too large" && exit 1; doneWhat you learned
You automated quality checks with Git hooks — pre-commit, commit-msg, and pre-push are the standard way to enforce conventions before code is shared.
Related Resources
Related Lessons
- Commit History
Hooks enforce commit conventions.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.