Skip to main content
This cookbook shows how to use hooks to enforce Git workflows, validate commits, protect branches, and automate changelog generation.

How it works

Git workflow hooks can:
  1. Validate commits: Check commit messages follow conventions
  2. Protect branches: Prevent accidental commits to main/production
  3. Generate changelogs: Auto-create changelog entries from commits
  4. Run pre-push checks: Validate code before pushing
  5. 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:
Add to .factory/hooks.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:
Add to PostToolUse:

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:
Problem: Both Droid hooks and .git/hooks running Solution: Coordinate or choose one:
Problem: Tests take too long Solution: Run quick checks only:

See also