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
bunif no lock file found - If a linter like Biome is installed, runs
{pm} run formatcommand
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 changesci- CI/CD configurationfeat- New featuresfix- Bug fixesperf- Performance improvementsrefactor- Code refactoringstyle- Code style changesdocs- Documentationtest- Testschore- Maintenance tasksrevert- Revert commitsworkflow- Workflow changeswip- Work in progresssecurity- 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 diffshows 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-verifyto prevent infinite loop
- Stages all changes with
- If no changes detected:
- Skips amend and logs message
- Checks if
- Use Case: Ensures formatted code is included in the commit without manual intervention

