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:
Want to master this scenario in a live sandbox? KodeKloud's Advanced Git Workflows, Rebase & Interactive Diagnostics covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "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."
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.
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.
- 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.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Running raw git push --force rather than git push --force-with-lease on shared team feature branches when configuring team wants enforce
git push --forceunconditionally overwrites remote commits made by teammates, whereas--force-with-leaseaborts if the remote ref was updated. - Resolving complex merge conflicts with blind checkout overwrite strategies without running unit tests when configuring team wants enforce Unilateral conflict resolution frequently clobbers complementary logic introduced in parallel feature branches, creating regression bugs.
-
Discarding corrupted working directories prematurely instead of recovering lost state using git reflog when configuring team wants enforce
git reflogtracks every HEAD movement, allowing instant recovery of discarded commits, botched rebases, or deleted local branches. - Disabling branch protection rules and mandatory PR approval reviews on shared release branches when configuring team wants enforce Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.