The Hook (The "Byte-Sized" Intro)
Good habits are hard to maintain. Hooks turn them into automatic habits. Format code before commit? Automatic. Check commit messages? Automatic. Run tests before push? Automatic. Here are the most popular hook use cases that teams actually use โ not theoretical ones, but the ones that save real time every day.
๐ What are Common Hook Use Cases?
The most popular and practical applications of Git hooks in real-world development workflows.
Conceptual Clarity
Top use cases by hook:
| Hook | Use Case | Tool |
|---|---|---|
pre-commit | Auto-format staged code | Prettier, Black |
pre-commit | Lint staged files | ESLint, Flake8 |
pre-commit | Prevent secrets from being committed | git-secrets, gitleaks |
pre-commit | Check for debug statements | grep for console.log, debugger |
commit-msg | Enforce conventional commits | commitlint |
commit-msg | Require ticket numbers | Regex check |
pre-push | Run test suite | Jest, pytest |
post-merge | Install new dependencies | npm install |
post-checkout | Rebuild when switching branches | Build script |
lint-staged: the magic combo:
lint-staged + pre-commit = only lint/format the files you actually changed. No wasted time on untouched files.
Real-Life Analogy
Hooks are like checklists built into a process. Airline pilots don't trust memory โ they follow a pre-flight checklist every time. Hooks are the developer's pre-flight checklist, automated and mandatory.
Visual Architecture
Why It Matters
- Consistent formatting: No more "fix formatting" commits.
- Security: Prevent accidental secret commits (API keys, passwords).
- Message quality: Conventional commits enable automated changelogs.
- Confidence: Tests passing locally before push reduces CI failures.
Code
# โโโ lint-staged setup (with Husky) โโโ
npm install --save-dev lint-staged
# package.json:
# "lint-staged": {
# "*.{js,ts}": ["eslint --fix", "prettier --write"],
# "*.css": "prettier --write"
# }
# .husky/pre-commit:
echo "npx lint-staged" > .husky/pre-commit
# โโโ Block secrets with gitleaks โโโ
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/sh
gitleaks protect --staged
if [ $? -ne 0 ]; then
echo "โ Secrets detected! Remove them before committing."
exit 1
fi
EOF
# โโโ Auto-install deps after merge โโโ
cat > .git/hooks/post-merge << 'EOF'
#!/bin/sh
# Check if package.json changed
CHANGED=$(git diff-tree -r --name-only ORIG_HEAD HEAD)
if echo "$CHANGED" | grep -q "package.json"; then
echo "๐ฆ package.json changed. Running npm install..."
npm install
fi
EOF
chmod +x .git/hooks/post-mergeKey Takeaways
- lint-staged + pre-commit is the most popular combo โ format/lint only staged files.
- Secret detection in pre-commit prevents accidental credential leaks.
- post-merge auto-installs deps when
package.jsonchanges. - Hooks turn good practices into automatic, enforced habits.
Interview Prep
-
Q: What is the most common use case for Git hooks? A: Pre-commit hooks that run
lint-stagedto automatically lint and format only the staged files. This ensures consistent code style without slowing down the workflow. -
Q: How do you prevent secrets from being committed? A: Use a pre-commit hook with a tool like
gitleaksorgit-secretsthat scans staged changes for patterns matching API keys, passwords, and tokens. -
Q: How do you auto-install dependencies after pulling new changes? A: A
post-mergehook that checks ifpackage.json(or equivalent) changed and runsnpm installautomatically.