Git Hooks 允許你在本機或伺服器端於特定 Git 事件發生前後執行腳本,自動化檢查、格式化、測試與部署流程。本文介紹常見 Hook 類型、使用時機與實戰範例 (Shell、Husky、Server-Side)。

一、Git Hooks 分類

節點 本機 (Client-Side) 伺服器端 (Server-Side)
Commit pre-commit, prepare-commit-msg, commit-msg, post-commit pre-receive, update, post-receive
Push pre-push post-update
其他 pre-rebase, pre-merge-commit, applypatch-msg, post-checkout

Hooks 皆位於 repo/.git/hooks/,初始以 .sample 結尾,去掉副檔名並加執行權限即可啟用。

二、本機 Hook 實戰

1. pre-commit:Lint & Test

1
2
3
4
5
6
7
8
#!/bin/sh
# .git/hooks/pre-commit
npm run lint
npm test
if [ $? -ne 0 ]; then
echo "❌ Lint/Test 失敗,禁止提交" >&2
exit 1
fi
  • 目的:阻止不合規的程式碼進入版本庫。
  • 常配合工具:golangci-lint, eslint, prettier

2. commit-msg:驗證訊息格式 (Conventional Commits)

1
2
3
4
5
6
7
#!/usr/bin/env node
const msg = require('fs').readFileSync(process.env.GIT_PARAMS, 'utf8').trim();
const regex = /^(feat|fix|docs|chore)(\(.+\))?: .{1,50}$/;
if (!regex.test(msg)) {
console.error('\n❌ commit 訊息不符合 Conventional Commits\n');
process.exit(1);
}

3. pre-push:執行 e2e 測試 & 版本標籤檢查

1
2
3
4
5
6
7
8
9
10
#!/bin/sh
npm run e2e
if [ $? -ne 0 ]; then
echo "❌ e2e 測試失敗,禁止 push" >&2
exit 1
fi

git tag --contains HEAD | grep -q 'v' && {
echo "✅ 已包含版本標籤"
}

三、Husky:跨平台管理 Hook

1
2
3
4
5
npx husky-init && npm install
# package.json
"scripts": { "prepare": "husky install" }
# 建立 hook
npx husky add .husky/pre-commit "npm run lint"

優點:

  1. Hooks 版本化,團隊開箱即用。
  2. Windows/macOS/Linux 均可運行。

四、伺服器端 Hook 實戰 (CI/CD)

pre-receive:拒絕直推 main

1
2
3
4
5
6
7
8
#!/bin/bash
while read oldrev newrev refname; do
if [[ "$refname" == "refs/heads/main" ]]; then
echo "❌ 請先開 PR,禁止直接推 main" >&2
exit 1
fi
done
exit 0

post-receive:自動部署

1
2
3
#!/bin/bash
GIT_WORK_TREE=/var/www/app git checkout -f main
systemctl restart app.service

五、最佳實踐

  1. 保持 Hook 快速:長時間任務移至 CI;本機 Hook ≤ 1–2 秒。
  2. 版本控制:使用 Husky 或 git config core.hooksPath 指向專案目錄。
  3. 可跳過快速修補git commit --no-verify;僅限緊急狀況。
  4. 安全性:Server-Side Hook 腳本要最小權限執行,避免執行未驗證輸入。
  5. 日誌 & 告警:Server-Side 建議將輸出導向 syslog,利於排查。

六、結語

Git Hooks 讓 DevOps 自動化從 提交第一步 就開始,把關程式碼品質並加速交付。依團隊流程挑選合適的 Hook,搭配 Husky 或 CI 腳本,即可打造無縫且可靠的開發體驗。

下一次開發前,不妨設定 pre-commit Lint 與 commit-msg 格式檢查,讓品質把關自動進行!