How it works
Git workflow hooks can:- Validate commits: Check commit messages follow conventions
- Protect branches: Prevent accidental commits to main/production
- Generate changelogs: Auto-create changelog entries from commits
- Run pre-push checks: Validate code before pushing
- Enforce PR requirements: Check branch names, linear issues, etc.
Prerequisites
Basic Git tools:Basic Git hooks
Commit message validation
Enforce conventional commit format. Create.factory/hooks/validate-commit-msg.sh:
.factory/settings.json:
Branch protection
Prevent commits directly to protected branches. Create.factory/hooks/protect-branches.sh:
Enforce branch naming
Require feature branches to follow naming conventions: Create.factory/hooks/validate-branch-name.sh:
Advanced Git automation
Auto-generate changelog entries
Automatically create changelog entries from commits: Create.factory/hooks/update-changelog.sh:
Pre-push validation
Run tests and checks before allowing git push: Create.factory/hooks/pre-push-check.sh:
Auto-create PR when pushing new branch
Automatically open a PR when pushing a feature branch: Create.factory/hooks/auto-create-pr.sh:
Enforce co-authorship in commits
Add co-author trailers to commits: Create.factory/hooks/add-coauthor.sh:
Real-world examples
Example 1: Monorepo commit validation
Ensure commits only touch one package: Create.factory/hooks/validate-monorepo-scope.sh:
Example 2: Release automation
Auto-tag releases when version changes: Create.factory/hooks/auto-tag-release.sh:
Best practices
1
Use PreToolUse for prevention
Block bad commits before they happen:
2
Use PostToolUse for automation
Automate followup actions after successful commits:
3
Provide clear error messages
Tell users exactly whatβs wrong and how to fix it:
4
Make hooks configurable
Allow teams to customize behavior:
5
Test hooks with dry-run
Test without making actual commits:
Troubleshooting
Problem: Validation too strict Solution: Add escape hatches:See also
- Hooks reference - Complete hooks API documentation
- Get started with hooks - Basic hooks introduction
- Code validation - Validate code quality
- Testing automation - Automate tests
