All chapters in this track
- 011. The Mental Model
- 022. Essential Setup
- 033. Core Daily Workflow
- 044. Branching
- 055. Merging & Conflict Resolution
- 066. Rebasing
- 077. Remote Repositories
- 088. Stashing
- 099. Undoing Things
- 1010. Tagging & Versioning
- 1111. Cherry-Pick
- 1212. Git Bisect & Blame
- 1313. Git Hooks
- 1414. Submodules
- 1515. Team Workflows
- 1616. Power Commands & Aliases
- 1717. Reflog: The Safety Net
- 1818. Practice Tasks
- 19Quick Reference Cheatsheet
Chapter 13 of 19
13. Git Hooks
Hooks are scripts Git runs automatically at certain events. Located in .git/hooks/.
Most useful hooks
| Hook | Runs when | Common use |
|---|---|---|
pre-commit | Before commit is created | Lint, format, run tests |
commit-msg | After writing commit message | Validate conventional commits format |
pre-push | Before git push | Run full test suite |
post-merge | After git pull/merge | Run npm install if package.json changed |
Example: pre-commit lint check
# .git/hooks/pre-commit (chmod +x this file)
#!/bin/sh
echo "Running linter..."
npm run lint
if [ $? -ne 0 ]; then
echo "Lint failed. Commit aborted."
exit 1
fi
Sharing hooks with the team
.git/hooks/ is not committed. To share hooks:
# Option 1: store in repo, configure path
git config core.hooksPath .githooks
# Option 2: use a tool like Husky (JS projects)
npm install husky --save-dev
npx husky init