Husky - Git Hooks Made Easy
Ce contenu n’est pas encore disponible dans votre langue.
Husky is a tool that makes Git hooks easy to use and manage. It allows you to run scripts automatically before commits, pushes, and other Git operations, helping you enforce code quality, run tests, and maintain consistent standards across your team.
📦 Installation
Section titled “📦 Installation”Install husky via npm, yarn, or pnpm:
npm install --save-dev husky# oryarn add --dev husky# orpnpm add --save-dev husky🔧 Basic Usage
Section titled “🔧 Basic Usage”1. Initialize Husky
Section titled “1. Initialize Husky”npx husky initThis creates a .husky directory with a sample pre-commit hook.
2. Create Your First Hook
Section titled “2. Create Your First Hook”Add a pre-commit hook to run linting:
echo "npm run lint" > .husky/pre-commit3. Configure Package Scripts
Section titled “3. Configure Package Scripts”Add helpful scripts to your package.json:
{ "scripts": { "prepare": "husky", "lint": "eslint .", "test": "vitest run" }}✨ Advanced Features
Section titled “✨ Advanced Features”Multiple Hooks
Section titled “Multiple Hooks”Create different hooks for different Git operations:
# Pre-commit: lint and formatecho "npm run lint && npm run format" > .husky/pre-commit
# Pre-push: run testsecho "npm test" > .husky/pre-push
# Commit-msg: validate commit messageecho "npx commitlint --edit $1" > .husky/commit-msgBypass Hooks Temporarily
Section titled “Bypass Hooks Temporarily”Sometimes you need to skip hooks:
git commit --no-verify -m "emergency fix"Integration with lint-staged
Section titled “Integration with lint-staged”Combine Husky with lint-staged for optimal performance:
npm install --save-dev lint-stagedAdd to package.json:
{ "lint-staged": { "*.{js,jsx,ts,tsx}": ["eslint --fix", "prettier --write"], "*.{json,md}": ["prettier --write"] }}Update .husky/pre-commit:
npx lint-stagedCustom Hook Scripts
Section titled “Custom Hook Scripts”Create more complex hooks with shell scripts:
#!/bin/sh. "$(dirname "$0")/_/husky.sh"
# Run type checkingnpm run type-check || exit 1
# Run tests for changed filesnpm run test:changed || exit 1
echo "✅ All checks passed!"📚 Best Practices
Section titled “📚 Best Practices”- Keep Hooks Fast: Only run checks on staged files to avoid slowing down commits
- Use lint-staged: Process only changed files for better performance
- Document Your Hooks: Add comments explaining what each hook does
- Team Alignment: Ensure all team members understand and agree on hook policies
- Graceful Failures: Provide clear error messages when hooks fail
- CI/CD Integration: Run the same checks in your CI pipeline as backup
🎯 Common Use Cases
Section titled “🎯 Common Use Cases”Enforce Code Quality
Section titled “Enforce Code Quality”npm run lintnpm run format:checknpm run type-checkPrevent Bad Commits
Section titled “Prevent Bad Commits”npx commitlint --edit $1Run Tests Before Push
Section titled “Run Tests Before Push”npm run test:coverageAutomatic Dependency Updates
Section titled “Automatic Dependency Updates”npm install📖 Official Documentation
Section titled “📖 Official Documentation”For more details, check out the Husky official documentation.