Q: Your team wants to enforce coding standards and prevent certain mistakes at commit time — for example, blocking commits with `console.log()` or failing if unit tests don't pass. How would you set this up with Git hooks?
Git hooks are scripts that run at specific points in the Git workflow. For pre-commit enforcement:
#Git #From your main checkout (on feature/new-api): #L2 #Version Control #Collaboration
🎙️ Candidate Opening & Architectural Context
""When an engineer accidentally creates this branch or commit divergence, I walk them through safe recovery without data loss. The interviewer is testing: Understanding of Git hooks, client-side vs server-side, and tooling like Husky.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third.""
Advertisement
🛠️ Production Runbook & Step-by-Step Resolution
1️⃣
Initial Diagnostics & Root Cause Analysis
Git hooks are scripts that run at specific points in the Git workflow. For pre-commit enforcement:
pre-commithook — runs before the commit is created. Exit non-zero to block the commit:commit-msghook — validates the commit message format (e.g., enforce Conventional Commits or JIRA ticket references):pre-pushhook — run tests before allowing a push.- Husky (Node.js) — stores hooks in
.husky/in the repo and installs them vianpm prepare.
2️⃣
Remediation & Permanent Safeguards
The distribution problem: hooks live in .git/hooks/, which is not committed to the repo. Solutions: Client-side hooks can be bypassed with git commit --no-verify. For enforcement you can't skip, use server-side hooks (pre-receive, update) on the Git server, or platform-based checks (GitHub required status checks, GitLab push rules).
#!/bin/sh
# .git/hooks/pre-commit
if git diff --cached --name-only | xargs grep -l 'console.log'; then
echo "ERROR: Remove console.log() before committing"
exit 1
fi
- pre-commit framework (Python) —
.pre-commit-config.yamldefines hooks from shared repos. - lefthook (Go) — similar, language-agnostic, fast.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: pre-commit hook — runs before the commit is created. Exit non-zero to block the commit:."
⚡ 60-Second Elevator Pitch Talking Points
- pre-commit hook — runs before the commit is created. Exit non-zero to block the commit:
- commit-msg hook — validates the commit message format (e.g., enforce Conventional Commits or JIRA...
- pre-push hook — run tests before allowing a push.
Advertisement