Git Hooks Setup

aj-geddes/useful-ai-prompts/skills/git-hooks-setup

作者 aj-geddes3f5182cfd739無授權條款355 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫7 個月前更新

Implement Git hooks using Husky, pre-commit, and custom scripts. Enforce code quality, linting, and testing before commits and pushes.

AI 產生的概覽

使用 Husky、pre-commit 與自訂指令碼設定 Git 掛鉤,在提交與推送前強制執行程式碼品質檢查。

功能
此技能會引導設定 Git 掛鉤,在提交與推送前執行自動化檢查。內容涵蓋 Husky 安裝、Node.js 與 Python 的 pre-commit 掛鉤、以 commitlint 驗證提交訊息、機密偵測,以及推送前執行測試。它附帶參考指南、測試鷹架指令碼與測試範本。
適用情境
適用於為儲存庫加入提交前程式碼檢查、格式化或機密掃描,驗證提交訊息規範,或在推送前執行測試。適合希望透過 Git 掛鉤一致落實程式碼品質標準的團隊。
執行需求
需要 Git,以及用於 Husky 與 commitlint 的 Node.js/npm 環境,或用於 Python 專案的 Python 與 pre-commit 框架。此技能附帶可執行指令碼(scripts/scaffold-tests.sh)與範本(templates/test-template.js)。

Git Hooks Setup

Table of Contents

Overview

Configure Git hooks to enforce code quality standards, run automated checks, and prevent problematic commits from being pushed to shared repositories.

When to Use

  • Pre-commit code quality checks
  • Commit message validation
  • Preventing secrets in commits
  • Running tests before push
  • Code formatting enforcement
  • Linting configuration
  • Team-wide standards enforcement

Quick Start

Minimal working example:

bash
#!/bin/bash# setup-husky.sh
# Install Huskynpm install husky --save-dev
# Initialize Huskynpx husky install
# Create pre-commit hooknpx husky add .husky/pre-commit "npm run lint"
# Create commit-msg hooknpx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
# Create pre-push hooknpx husky add .husky/pre-push "npm run test"
# Create post-merge hooknpx husky add .husky/post-merge "npm install"

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
Husky Installation and Configuration [blocked]Husky Installation and Configuration
Pre-commit Hook (Node.js) [blocked]Pre-commit Hook (Node.js)
Commit Message Validation [blocked]Commit Message Validation
Commitlint Configuration [blocked]Commitlint Configuration, Pre-push Hook (Comprehensive)
Pre-commit Framework (Python) [blocked]Pre-commit Framework (Python)
Secret Detection Hook [blocked]Secret Detection Hook, Husky in package.json

Best Practices

✅ DO

  • Enforce pre-commit linting and formatting
  • Validate commit message format
  • Scan for secrets before commit
  • Run tests on pre-push
  • Skip hooks only with --no-verify (rarely)
  • Document hook requirements in README
  • Use consistent hook configuration
  • Make hooks fast (< 5 seconds)
  • Provide helpful error messages
  • Allow developers to bypass with clear warnings

❌ DON'T

  • Skip checks with --no-verify
  • Store secrets in committed files
  • Use inconsistent implementations
  • Ignore hook errors
  • Run full test suite on pre-commit

來源與署名

來源:aj-geddes/useful-ai-prompts位於skills/git-hooks-setup提交3f5182c

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架