客户端 Git 钩子
了解并实现 `pre-commit` 和 `post-merge` 等客户端钩子,在本地强制执行规范
客户端 Git 钩子 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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:
- Remove the
.sampleextension from its filename. - 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
fiTesting 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)"
fiHook 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/hooksdirectory. - How to activate: Rename from
.sampleand 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 钩子」课时是免费的吗?
是的 — 「客户端 Git 钩子」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。
「客户端 Git 钩子」这节课中我会学到什么?
了解并实现 `pre-commit` 和 `post-merge` 等客户端钩子,在本地强制执行规范 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「客户端 Git 钩子」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?
能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 客户端 Git 钩子
- 服务器端 Git 钩子
- Git 配置与别名
- 使用 Husky 与团队共享钩子