Skip to content

Git Hooks

Catch validation errors before they reach the repository by running Repotype in git hooks.

Terminal window
npx repotype install-checks --hook both --target .

This installs both pre-commit and pre-push hooks.

OptionEffect
--hook pre-commitValidate before each commit
--hook pre-pushValidate before pushing
--hook bothInstall both hooks

Define hooks in your config file:

repotype.yaml
operations:
hooks:
enabled: true
hook: both # pre-commit | pre-push | both

Then apply:

Terminal window
npx repotype apply .

The installed hooks run:

Terminal window
npx repotype validate . --json

If validation fails, the commit/push is blocked with error output.

If you prefer manual control, add to .git/hooks/pre-commit:

#!/bin/sh
npx repotype validate . --json
exit $?

Make it executable:

Terminal window
chmod +x .git/hooks/pre-commit

If you use Husky for hook management:

  1. Install Husky

    Terminal window
    npm install -D husky
    npx husky init
  2. Add Repotype to pre-commit

    Terminal window
    echo "npx repotype validate ." > .husky/pre-commit

For faster commits, only validate changed files:

package.json
{
"lint-staged": {
"*.{ts,tsx}": [
"npx repotype explain"
],
"*.md": [
"npx repotype validate"
]
}
}

In emergencies, you can skip hooks:

Terminal window
git commit --no-verify -m "emergency fix"
git push --no-verify

Remove hooks from config:

repotype.yaml
operations:
hooks:
enabled: false

Then reapply:

Terminal window
npx repotype apply .

Or manually remove:

Terminal window
rm .git/hooks/pre-commit .git/hooks/pre-push

Check hook is executable:

Terminal window
ls -la .git/hooks/pre-commit
# Should show: -rwxr-xr-x

Consider:

  • Using pre-push instead of pre-commit
  • Using lint-staged for file-specific validation
  • Caching node_modules
Terminal window
chmod +x .git/hooks/pre-commit
chmod +x .git/hooks/pre-push