The Hook (The "Byte-Sized" Intro)
A hook that only runs on your machine protects only you. When every developer has the same hooks, the entire team is protected. But hooks live in .git/hooks/ — not tracked, not shared on clone. The solution? Use core.hooksPath, Husky, or Git templates to make hooks portable: install automatically, run everywhere.
📖 What is Making Hooks Portable?
Techniques for ensuring Git hooks are automatically installed and consistent across all team members' machines.
Conceptual Clarity
Portability approaches:
| Approach | Setup Effort | Auto-Install? | Best For |
|---|---|---|---|
core.hooksPath | Low | ❌ Manual config | Small teams |
| Husky (npm) | Medium | ✅ On npm install | JS/TS projects |
| pre-commit (Python) | Medium | ✅ On install | Python projects |
| lefthook (Go) | Medium | ✅ On install | Any project |
| Git templates | Medium | 🟡 On clone/init | Organization-wide |
Why Husky is popular:
- Hooks are stored in
.husky/(tracked) - Auto-installed via
npm installprepare script - Zero config for developers — just clone and install
Real-Life Analogy
Individual hooks are like each team member having their own safety checklist. Portable hooks are like a safety checklist printed on the factory floor wall — everyone follows the same rules, automatically.
Visual Architecture
Why It Matters
- Consistency: Everyone runs the same checks — no "it works on my machine."
- Zero effort: Hooks install automatically on clone + install.
- Enforcement: Nobody accidentally skips setup.
- Maintainability: Hooks are versioned and reviewed in PRs.
Code
# ─── Method 1: core.hooksPath ───
mkdir .githooks
# Place your hooks in .githooks/
git config core.hooksPath .githooks
# Document in README: git config core.hooksPath .githooks
# ─── Method 2: Husky (recommended for JS) ───
npm install --save-dev husky
npx husky init
# Creates .husky/ directory with a pre-commit hook
# Add hooks:
echo "npx lint-staged" > .husky/pre-commit
echo 'npx commitlint --edit "$1"' > .husky/commit-msg
# package.json automatically has:
# "prepare": "husky"
# So `npm install` installs hooks automatically!
# ─── Method 3: lefthook (language-agnostic) ───
# Install: brew install lefthook
# lefthook.yml:
# pre-commit:
# commands:
# lint:
# run: npm run lint
# ─── Method 4: Git templates ───
git config --global init.templateDir ~/.git-templates
mkdir -p ~/.git-templates/hooks
# Place hooks in ~/.git-templates/hooks/
# Every `git init` and `git clone` copies themKey Takeaways
- Husky is the most popular solution for JavaScript projects.
core.hooksPathis the simplest approach — no dependencies.- lefthook and pre-commit work for non-JS ecosystems.
- The goal: hooks install automatically with zero developer effort.
Interview Prep
-
Q: How do you ensure all team members use the same Git hooks? A: Use a tool like Husky that auto-installs hooks during
npm install, or setcore.hooksPathto a tracked directory. The key is zero-effort setup — hooks should install automatically on clone. -
Q: What is Husky and how does it work? A: Husky is an npm package that manages Git hooks. It stores hooks in a
.husky/directory (tracked in Git) and auto-installs them via thepreparescript inpackage.json, which runs onnpm install. -
Q: How would you enforce hooks in a non-JavaScript project? A: Use
lefthook(Go-based, language-agnostic) orpre-commit(Python-based). Both support config files committed to the repo and auto-install on setup.