Git Hooks with Husky
FreshJuice DEV uses Husky to automate quality checks and maintain code consistency through git hooks.What is Husky?
Husky makes it easy to use git hooks as if they were npm scripts. It helps you:- Run tests before commits
- Lint and format code automatically
- Validate commit messages
- Prevent bad commits and pushes
- Notify team members of important changes
Installation
Husky is already configured in FreshJuice DEV. It auto-installs when you run:prepare script in package.json:21 handles initialization:
Active Hooks
FreshJuice DEV includes two pre-configured hooks:1. Pre-commit Hook
Automatically cleans module metadata before every commit. Location:.husky/pre-commit
What it does:
- Removes
module_idfrommeta.jsonfiles - Prevents merge conflicts from HubSpot-generated IDs
- Keeps version control clean and portable
- Requires
jq(JSON processor) to be installed - Install with:
brew install jq(macOS) orapt-get install jq(Linux)
2. Post-merge Hook
Notifies you when dependencies have changed after pulling/merging. Location:.husky/post-merge
What it does:
- Detects changes to
package-lock.json - Reminds you to run
npm install - Prevents version mismatch issues
Creating Custom Hooks
Step 1: Create Hook File
Create a new hook in.husky/:
.husky/pre-push:
Step 2: Make it Executable
Step 3: Test the Hook
Common Hook Patterns
Lint Staged Files
Run linting only on staged files: Install lint-staged:.husky/pre-commit:
package.json:
Validate Commit Messages
Enforce conventional commit format:.husky/commit-msg:
Run Tests Before Push
.husky/pre-push:
Build Theme Before Commit
.husky/pre-commit:
Check for Large Files
.husky/pre-commit:
Available Git Hooks
Commit Workflow
pre-commit: Before commit is createdprepare-commit-msg: Before commit message editor openscommit-msg: After commit message is savedpost-commit: After commit is created
Merge Workflow
pre-merge-commit: Before merge commit is createdpost-merge: After merge is completed
Push Workflow
pre-push: Before push to remotepost-push: After push to remote (requires setup)
Other Hooks
pre-rebase: Before rebasepost-checkout: After checkoutpost-rewrite: After amend or rebase
Best Practices
1. Keep Hooks Fast
Slow hooks interrupt workflow. Keep execution under 5 seconds:2. Provide Clear Error Messages
3. Allow Bypassing in Emergencies
Users can skip hooks with--no-verify:
4. Check Dependencies
Verify required tools are installed:5. Add to Documentation
Document your hooks in your README:Debugging Hooks
Enable Verbose Output
Add debugging to your hooks:Check Hook Execution
Verify hooks are running:View Hook Errors
Git shows hook output in terminal. Check for:- Permission errors (use
chmod +x) - Command not found (install dependencies)
- Syntax errors (test with
bash .husky/pre-commit)
Test Hooks Manually
Run hooks directly:Real-World Examples
Example 1: Theme Version Bump
Automatically bump version before release:.husky/pre-push:
Example 2: Sync with HubSpot
Upload changes to HubSpot after successful commit:.husky/post-commit:
SKIP_UPLOAD=true git commit -m "message"
Example 3: Clean Build Artifacts
.husky/post-checkout:
Disabling Hooks
Temporarily Disable
Skip hooks for one command:Disable All Hooks
Rename the.husky directory:
Disable Specific Hook
Rename the hook file:Troubleshooting
Hook Not Running
Check if Husky is installed:Permission Denied
Make hooks executable:Command Not Found
Install missing dependency:Hook Exits Without Error
Add explicit exit:CI/CD Integration
Hooks don’t run in CI/CD by default. Replicate checks: GitHub Actions example:Migration Guide
From Husky v4 to v8+
FreshJuice DEV uses Husky v9. If migrating from v4:-
Remove old configuration:
-
Install new version:
-
Initialize:
-
Migrate hooks:
Move from
package.jsonto.husky/directory
Next Steps
- Learn about Performance Optimization
- Explore Custom Sections
- Review Husky documentation
- Set up lint-staged
Resources
- Husky: Official Documentation
- Git Hooks: Git Documentation
- Conventional Commits: Specification