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 从公开仓库中收录这些内容。

举报或申请下架