Git Hooks
Automate code quality checks locally before your code is ever committed.
The Local Gatekeepers
Git hooks are custom scripts that Git automatically executes before or after critical lifecycle events such as committing (pre-commit), pushing (pre-push), or receiving code.
They allow developers to intercept the Git workflow to run custom logic, validate changes, or trigger external actions automatically.
Why it matters
For frontend developers, Git hooks are the ultimate automation tool for enforcing code quality.
By using hooks, you can automatically run linters (ESLint), format code (Prettier), and enforce commit structures before bad code makes it into the repository. It shifts quality control left, saving time in CI pipelines.
How it works (Husky)
Native Git hooks live in the hidden .git/hooks directory, which isn't shared with the team when they clone the repository.
To solve this, web teams use Husky. Husky configures Git to use a version-controlled directory (like .husky/) for its hooks. When developers run npm install, Husky automatically initializes these hooks so everyone has the exact same setup.
Inspect a commit hook
Compare a failing check, a corrected change and an explicit hook bypass in this simulation.
A pre-commit formatting check is installed in this fixture. Example CI formatting check: not run
Staged sample
const count=1
Does not match the fixture's formatting rule.
Commit result
No new commit yet.
Independent CI: not run
A failing pre-commit hook can abort a commit. --no-verify bypasses the configured pre-commit check here, but does not fix the content or bypass an independent CI check. Hooks must be installed/configured; this is a local simulation.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- Hooks are scripts that run on Git lifecycle events.
- Husky makes it easy to share hooks across a web team.
- Hooks provide fast local feedback (the spellchecker).
- Never rely solely on hooks for security; always use CI/CD.
Done with this concept?
Mark it complete to track your progress. No login needed.