Create FasterCreate Faster

Husky

Husky is a Git hooks manager that lets you run scripts at key moments in the Git workflow for quality assurance.

Husky makes Git hooks easy, allowing you to enforce code quality, commit message conventions, and automated formatting before commits.

Why use Husky?

  • Quality Gates: Prevent bad commits from entering the repository
  • Automation: Automatically format code and run checks
  • Convention Enforcement: Ensure commit messages follow standards
  • Easy Setup: Simple configuration with zero config needed
  • Fast: Lightweight shell scripts with minimal overhead
  • Team Consistency: Everyone uses the same hooks automatically

Requirements: Git repository

Technical Changes

Git Hooks Setup

  • Creates .husky/ directory at project root with three hooks

1. Pre-Commit Hook .husky/pre-commit

  • Purpose: Automatically format code before committing
  • Behavior:
    • Detects package manager (bun, pnpm, npm, or yarn) by checking lock files
    • Defaults to bun if no lock file found
    • If a linter like Biome is installed, runs {pm} run format command

2. Commit Message Hook .husky/commit-msg

  • Purpose: Validate commit message format follows conventional commits
  • Behavior:
    • Reads commit message from Git temporary file
    • Validates commit type prefix using regex
    • Accepted Types:
      • build - Build system changes
      • ci - CI/CD configuration
      • feat - New features
      • fix - Bug fixes
      • perf - Performance improvements
      • refactor - Code refactoring
      • style - Code style changes
      • docs - Documentation
      • test - Tests
      • chore - Maintenance tasks
      • revert - Revert commits
      • workflow - Workflow changes
      • wip - Work in progress
      • security - Security fixes
    • Expected Format: <type>(<scope>): <subject>
    • Example: feat(auth): add email login
    • Shows warning with emoji guide if invalid, but allows commit to proceed

3. Post-Commit Hook .husky/post-commit

  • Purpose: Auto-amend commit if formatting changed files
  • Behavior:
    • Checks if git diff shows changes after pre-commit formatting
    • If changes detected:
      • Stages all changes with git add .
      • Amends the commit with --no-edit --no-verify
      • Skips hooks with --no-verify to prevent infinite loop
    • If no changes detected:
      • Skips amend and logs message
  • Use Case: Ensures formatted code is included in the commit without manual intervention