Pre-commit Hooks
Scripts that run automatically before a git commit is created, used to enforce code quality standards, run linters, and prevent committing problematic code.
Definition
Pre-commit hooks are part of Git's hooks system (.git/hooks/pre-commit). They can block commits that fail checks. Common uses: running linters (ESLint, Prettier), checking for secrets, running unit tests, and enforcing commit message formats. Tools like Husky (Node.js), pre-commit (Python), and Lefthook make hook management easier. Hooks run locally on developer machines, catching issues before they reach CI.
Why It Matters
Pre-commit hooks shift quality checks left, catching issues before code is committed or pushed. This provides immediate feedback to developers and prevents polluting git history with "fix lint" commits. They complement (but don't replace) CI checks since developers could bypass local hooks.
Example
A developer tries to commit code with console.log statements. The pre-commit hook runs ESLint, which detects the no-console violation and blocks the commit with an error message explaining what needs to be fixed.