Lesson Completion
Back to course

Making Hooks Portable

Intermediate
7 minutes4.7Git

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:

ApproachSetup EffortAuto-Install?Best For
core.hooksPathLow❌ Manual configSmall teams
Husky (npm)Medium✅ On npm installJS/TS projects
pre-commit (Python)Medium✅ On installPython projects
lefthook (Go)Medium✅ On installAny project
Git templatesMedium🟡 On clone/initOrganization-wide

Why Husky is popular:

  • Hooks are stored in .husky/ (tracked)
  • Auto-installed via npm install prepare 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

flowchart TD CLONE["git clone + npm install"] --> HUSKY["🐶 Husky auto-installs hooks"] HUSKY --> PRECOMMIT["✅ pre-commit: lint"] HUSKY --> COMMITMSG["✅ commit-msg: format"] HUSKY --> PREPUSH["✅ pre-push: test"] style HUSKY fill:#0f3460,stroke:#53d8fb,color:#53d8fb

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

bash
# ─── 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 them

Key Takeaways

  • Husky is the most popular solution for JavaScript projects.
  • core.hooksPath is 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 set core.hooksPath to 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 the prepare script in package.json, which runs on npm install.

  • Q: How would you enforce hooks in a non-JavaScript project? A: Use lefthook (Go-based, language-agnostic) or pre-commit (Python-based). Both support config files committed to the repo and auto-install on setup.

Topics Covered

Git HooksTeam Workflow

Tags

#git#hooks#portable#team

Last Updated

2026-02-13