Git Hooks: Automate Code Quality Checks Before Every Commit
What Git Hooks Are
Git hooks are scripts that run automatically at specific points in the Git workflow. They live in .git/hooks/ and are not committed to the repository by default — so you need a way to share them with your team.
The most useful hooks:
| Hook | When it runs | Common use |
|---|---|---|
pre-commit | Before commit is created | Linting, formatting, tests |
commit-msg | After commit message is written | Enforce message format |
pre-push | Before push to remote | Run full test suite |
post-merge | After a merge | Install new dependencies |
---
Writing a pre-commit Hook
#!/usr/bin/env bash
# .git/hooks/pre-commit
set -euo pipefail
# Run ESLint on staged JS/TS files
STAGED=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|ts|jsx|tsx)