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

Git Hook ฝั่งไคลเอ็นต์

ทำความเข้าใจและนำฮุกฝั่งไคลเอ็นต์ เช่น `pre-commit` และ `post-merge` มาใช้เพื่อบังคับใช้มาตรฐานภายในเครื่อง

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

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro to Git Hooks

Welcome! In this lesson, you'll learn about Git hooks. These are scripts that Git automatically runs before or after events like committing, merging, or pushing.

Think of them as automated assistants for your Git workflow. They help enforce project standards and automate routine tasks.

Client-Side vs. Server-Side

Git hooks come in two flavors:

  • Client-side hooks: Run on your local machine. They only affect your personal repository. Examples include `pre-commit` and `post-merge`.
  • Server-side hooks: Run on the Git server. They affect all pushes to the repository. We'll cover these in a later lesson.

This lesson focuses on client-side hooks and how to use them locally.

Where Hooks Live

All Git hooks are stored in the .git/hooks directory within your repository. When you initialize a new Git repository, Git populates this directory with sample hook scripts.

Let's take a look inside a typical .git/hooks folder:

ls -F .git/hooks/

Activating a Hook

By default, the sample hook scripts end with .sample, making them inactive. To activate a hook, you need to:

  1. Remove the .sample extension from its filename.
  2. Make the script executable.

Let's activate the pre-commit hook by renaming and making it executable:

cd .git/hooks
mv pre-commit.sample pre-commit
chmod +x pre-commit

`pre-commit`: Your Local Guard

The pre-commit hook is one of the most useful client-side hooks. It runs just before Git asks you for a commit message and before the actual commit is created.

If the script exits with a non-zero status, Git aborts the commit. This makes it perfect for:

  • Running linters (e.g., ESLint, Flake8)
  • Executing unit tests
  • Checking for common mistakes (e.g., debug statements)

Example: `pre-commit` for Linting

Here's a simple pre-commit script that checks if any staged files contain the word "TODO". If it finds "TODO", the commit is blocked.

You can edit the .git/hooks/pre-commit file directly to add this content:

#!/bin/sh

# Check for 'TODO' in staged changes
if git diff --cached | grep -q "TODO"; then
  echo "Error: 'TODO' found in staged changes. Please resolve before committing."
  exit 1
fi

Testing the `pre-commit` Hook

Now, let's try to commit a file that contains "TODO". The pre-commit hook should prevent the commit.

First, create a file with "TODO", then try to commit it:

echo "This is a file with a TODO item." > my_feature.txt
git add my_feature.txt
git commit -m "Attempt to commit with TODO"

`post-merge`: After the Merge

The post-merge hook runs immediately after a successful git merge or git pull (which is essentially a fetch followed by a merge or rebase).

It's useful for automating tasks that should happen after your working directory has been updated by a merge, such as:

  • Installing new dependencies
  • Rebuilding your project
  • Updating documentation or symlinks

Example: `post-merge` for Dependencies

Imagine you're working on a project with Node.js. After merging a branch that introduced new dependencies (updated package.json), you'd want to run npm install.

Here's a post-merge script that checks if package.json was changed by the merge and runs npm install if it was:

#!/bin/sh

# Run npm install if package.json changed
if git diff --name-only HEAD@{1} HEAD | grep -q "package.json"; then
  echo "package.json changed, running npm install..."
  # npm install # Uncomment in a real project
  echo "(npm install simulated)"
fi

Hook Challenge

You've learned about client-side Git hooks! Which of the following statements are TRUE about them?

Client-Side Hooks: Recap

Great job! You've successfully explored client-side Git hooks. Here's what we covered:

  • What they are: Scripts that run automatically at specific Git events.
  • Where they live: In the .git/hooks directory.
  • How to activate: Rename from .sample and make executable.
  • `pre-commit`: Runs before a commit, useful for validation and linting.
  • `post-merge`: Runs after a merge, good for dependency updates or rebuilding.

Client-side hooks are powerful tools for personal workflow automation and local quality checks!

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

บทเรียน “Git Hook ฝั่งไคลเอ็นต์” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “Git Hook ฝั่งไคลเอ็นต์”

ทำความเข้าใจและนำฮุกฝั่งไคลเอ็นต์ เช่น `pre-commit` และ `post-merge` มาใช้เพื่อบังคับใช้มาตรฐานภายในเครื่อง คุณปฏิบัติ Git Advanced: Monorepo, Submodules & Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

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

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

บทเรียน “Git Hook ฝั่งไคลเอ็นต์” ใช้เวลานานแค่ไหน

บทเรียน 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