0Pricing
Git Advanced: Monorepo, Submodules & Workflows · 课时

使用 Husky 与团队共享钩子

本地 Git 钩子不受版本控制。了解 Husky 及类似工具如何将钩子分发给每位贡献者,从而一致地执行质量门禁。

使用 Husky 与团队共享钩子 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Distribution Problem

Git hooks live in .git/hooks, which is not part of the repository. So a pre-commit hook you write only runs on your machine.

To enforce standards across a team, hooks must be shareable and version-controlled.

The core.hooksPath Setting

Git can read hooks from a custom directory you commit to the repo via core.hooksPath. This is the foundation most tools build on.

git config core.hooksPath .githooks

Enter Husky

Husky is a popular tool for JavaScript projects that manages hooks via committed files and wires up core.hooksPath automatically on install.

npm install --save-dev husky

Initializing Husky

Run the init command. It creates a .husky/ directory and a sample hook, and sets up the prepare script.

npx husky init

The prepare Script

Husky relies on npm's prepare lifecycle so hooks are enabled automatically whenever a teammate runs npm install.

{
  "scripts": {
    "prepare": "husky"
  }
}

Writing a Pre-Commit Hook

Add commands to .husky/pre-commit. Since the file is committed, every contributor gets the same checks.

npm test
npm run lint

Running Only on Staged Files

Linting the whole project on every commit is slow. lint-staged runs tools only on the files you actually staged.

npx lint-staged

Configuring lint-staged

Map file patterns to commands. Below runs the formatter and linter only on staged JS files.

{
  "lint-staged": {
    "*.js": ["prettier --write", "eslint --fix"]
  }
}

Enforcing Commit Messages

A commit-msg hook can validate message format. Paired with commitlint, it enforces conventional commits across the team.

npx --no -- commitlint --edit $1

Tools for Other Ecosystems

Husky is Node-centric. Language-agnostic options include pre-commit (Python), lefthook (Go, very fast), and overcommit (Ruby). All solve the same distribution problem.

Hooks Are Not a Security Boundary

Remember: client hooks can be bypassed with --no-verify. They catch honest mistakes, but real enforcement must also live in CI and server-side hooks.

git commit --no-verify

Quick Check

Test your shared-hooks knowledge.

Recap

You can now share hooks across a team:

  • core.hooksPath points Git at a committed hooks directory
  • Husky automates setup via the prepare script
  • lint-staged runs checks only on staged files
  • commitlint enforces message format
  • Hooks complement, never replace, server-side and CI enforcement

This builds directly on your client-side and server-side hook lessons.

常见问题解答

「使用 Husky 与团队共享钩子」课时是免费的吗?

是的 — 「使用 Husky 与团队共享钩子」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

「使用 Husky 与团队共享钩子」这节课中我会学到什么?

本地 Git 钩子不受版本控制。了解 Husky 及类似工具如何将钩子分发给每位贡献者,从而一致地执行质量门禁。 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 Husky 与团队共享钩子」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?

能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 客户端 Git 钩子
  2. 服务器端 Git 钩子
  3. Git 配置与别名
  4. 使用 Husky 与团队共享钩子
← 返回 Git Advanced: Monorepo, Submodules & Workflows