Manuals / Git & GitHub / Chapter 10
D · Automate · advanced · Day 7–8 · Chapter 10 of 12
Git hooks — pre-commit & pre-push
Hooks run scripts at Git lifecycle events. pre-commit: lint/format. pre-push: tests. Husky or plain .git/hooks.
Path progress78%
Step 1 of 3
Simple pre-commit hook
Executable script in .git/hooks/pre-commit. Exit 1 blocks commit. Test in practice repo.
Example
#!/bin/sh
# .git/hooks/pre-commit
if ! grep -q "." README.md 2>/dev/null; then
echo "README must not be empty"
exit 1
fiDo this now
Hook that rejects commits if README is empty.
Was this step clear?
Chapter learning outcomes
- Hook scripts in .git/hooks
- pre-commit framework
- Husky for Node projects
- CI vs local hooks
Clear these before you leave
Side quest
pre-push tests
pre-push hook runs pytest or npm test before push.