Git Advanced: Monorepo, Submodules & Workflows · บทเรียน

การแชร์ฮุกกับทีมด้วย Husky

ฮุก Git ภายในเครื่องอยู่นอกระบบควบคุมเวอร์ชัน เรียนรู้ว่า Husky และเครื่องมือที่คล้ายกันกระจายฮุกไปยังผู้ร่วมพัฒนาทุกคนอย่างไร เพื่อให้ด่านตรวจคุณภาพทำงานอย่างสม่ำเสมอ

บทเรียน 4 จาก 413 ขั้นตอน

การแชร์ฮุกกับทีมด้วย Husky เป็นบทเรียน Git Advanced: Monorepo, Submodules & Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

เริ่มต้นได้ฟรี

เรียนรู้ Git Advanced: Monorepo, Submodules & Workflows ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การแชร์ฮุกกับทีมด้วย Husky” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแชร์ฮุกกับทีมด้วย Husky” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Git Advanced: Monorepo, Submodules & Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Git Advanced: Monorepo, Submodules & Workflows มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแชร์ฮุกกับทีมด้วย Husky”

ฮุก Git ภายในเครื่องอยู่นอกระบบควบคุมเวอร์ชัน เรียนรู้ว่า Husky และเครื่องมือที่คล้ายกันกระจายฮุกไปยังผู้ร่วมพัฒนาทุกคนอย่างไร เพื่อให้ด่านตรวจคุณภาพทำงานอย่างสม่ำเสมอ คุณปฏิบัติ Git Advanced: Monorepo, Submodules & Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Git Advanced: Monorepo, Submodules & Workflows หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Git Advanced: Monorepo, Submodules & Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การแชร์ฮุกกับทีมด้วย Husky” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Git Advanced: Monorepo, Submodules & Workflows นี้ได้ไหม

ได้ บทเรียน Git Advanced: Monorepo, Submodules & Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Git Hook ฝั่งไคลเอ็นต์
  2. Git Hook ฝั่งเซิร์ฟเวอร์
  3. การกำหนดค่าและชื่อแทนของ Git
  4. การแชร์ฮุกกับทีมด้วย Husky
← กลับไปที่ Git Advanced: Monorepo, Submodules & Workflows