Skip to content
Back to AI Coding

Pre-Commit AI Hooks

Run a fast AI pass in a pre-commit hook: fix typos, add missing types, format.

AutomationIntermediate

Why

GitHub Copilot reached ~$400M ARR growing 281% YoY and accounted for 40% of net new GitHub revenue in 2024, demonstrating automation ROI at scale. A pre-commit AI hook catches small issues before they hit review: missing types, obvious typos, unformatted code.

It's cheap (only staged files) and keeps the repo tidy without a separate pass.

When

Use when you want zero-touch cleanup on every commit. Skip if commits are slow or the team prefers explicit cleanup.

How

1. Add a pre-commit hook (husky/lefthook) that runs an AI fixer on staged files. 2. Scope to safe transforms only: types, formatting, typo fixes. 3. Re-stage the AI's changes; abort if it changes semantics. 4. Keep it under ~5s; skip on large diffs. 5. Log what it changed for audit.

Example

# .husky/pre-commit npx ai-fix --staged --safe-only --re-stage --timeout 5000 # ai-fix adds missing return types, fixes obvious typos, runs prettier. # Aborts if a change isn't in the safe-only allowlist. git commit -m "feat: add books route" # hook runs, fixes, re-stages, commits.

Common Problems

  • ⚠GitHub Actions: "Process completed with exit code 1" → check the action log line with `##[error]`; common cause is a missing env var (set in repo Secrets).
  • ⚠Error: EADDRINUSE: address already in use :::3000 → run `npx kill-port 3000` or change the port in config.
  • ⚠Error: Cron job silently fails → wrap in `set -e; ...` and redirect output to a log; verify with `tail -f /var/log/job.log`.

Best Practices

  • ✓Restrict to a safe-transform allowlist (types, format, typos).
  • ✓Re-stage and verify the diff is non-semantic before committing.
  • ✓Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved.

FAQ

What is Pre-Commit AI Hooks?

Run a fast AI pass in a pre-commit hook: fix typos, add missing types, format. It helps developers quickly grasp the core value and use cases of Pre-Commit AI Hooks.

How do I get started with Pre-Commit AI Hooks?

To get started with Pre-Commit AI Hooks, focus on these points first: Add a pre-commit hook (husky/lefthook) that runs an AI fixer on staged files.

What are common mistakes with Pre-Commit AI Hooks?

Common mistakes when using Pre-Commit AI Hooks include: GitHub Actions: "Process completed with exit code 1" → check the action log line with `##[error]`; common cause is a missing env var (set in repo Secrets).; Error: EADDRINUSE: address already in use :::3000 → run `npx kill-port 3000` or change the port in config.; Error: Cron job silently fails → wrap in `set -e; ...` and redirect output to a log; verify with `tail -f /var/log/job.log`..

What are the best practices for Pre-Commit AI Hooks?

Best practices for Pre-Commit AI Hooks include: Restrict to a safe-transform allowlist (types, format, typos).; Re-stage and verify the diff is non-semantic before committing.; Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved..

How do I learn more about Pre-Commit AI Hooks?

Start with the official docs, then search real-world examples on GitHub. Validate patterns in a small scratch repo before applying them to production code.