Git hooks

From InugamiDev/ultrathink-oss · View interactive version

Git hooks is an AI agent skill from the GitHub repository InugamiDev/ultrathink-oss. Git hooks with Husky setup, pre-commit with lint-staged, commit-msg with commitlint, pre-push checks, and conventional commits enforcement

How to use this skill

Skill file

Git Hooks Skill

Purpose

Automate code quality enforcement at commit and push time. Catch lint errors, formatting issues, and bad commit messages before they enter the repository -- not in CI where feedback is slow.

Setup

Husky + lint-staged + commitlint (Full Stack)

# Install dependencies
pnpm add -D husky lint-staged @commitlint/cli @commitlint/config-conventional

# Initialize husky
pnpm exec husky init

Package.json Config

{
  "scripts": {
    "prepare": "husky"
  },
  "lint-staged": {
    "*.{ts,tsx}": [
      "eslint --fix --max-warnings 0",
      "prettier --write"
    ],
    "*.{json,md,css}": [
      "prettier --write"
    ]
  }
}

Hook Scripts

Pre-Commit: Lint Staged Files Only

# .husky/pre-commit
pnpm exec lint-staged
// lint-staged.config.ts — advanced config with type checking
import type { Config } from "lint-staged";

const config: Config = {
  "*.{ts,tsx}": (filenames) => [
    `eslint --fix --max-warnings 0 ${filenames.join(" ")}`,
    `prettier --write ${filenames.join(" ")}`,
    "tsc --noEmit --pretty",  // Type check entire project (not per-file)
  ],
  "*.css": ["prettier --write"],
  "*.{json,md}": ["prettier --write"],
};

export default config;

Commit-Msg: Enforce Conventional Commits

# .husky/commit-msg
pnpm exec commitlint --edit $1
// commitlint.config.ts
import type { UserConfig } from "@commitlint/types";

const config: UserConfig = {
  extends: ["@commitlint/config-conventional"],
  rules: {
    "type-enum": [
      2,
      "always",
      ["feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore", "revert"],
    ],
    "subject-max-length": [2, "always", 72],
    "body-max-line-length": [1, "always", 100],
  },
};

export default config;

Conventional Commit Format

type(scope): description

feat(auth): add OAuth 2.0 login flow
fix(cart): resolve quantity update race condition
docs(api): update endpoint documentation
refactor(db): extract query builder utility
perf(images): lazy load below-fold images
test(checkout): add integration tests for payment flow

Pre-Push: Run Tests and Type Check

# .husky/pre-push
pnpm run typecheck
pnpm run test:ci

Advanced Patterns

Skip Hooks When Needed

# Emergency bypass (use sparingly)
git commit --no-verify -m "hotfix: critical production fix"
git push --no-verify

Branch Name Validation

# .husky/pre-push
BRANCH=$(git rev-parse --abbrev-ref HEAD)
PATTERN="^(feat|fix|hotfix|chore|refactor)/[a-z0-9._-]+$"

if [[ ! "$BRANCH" =~ $PATTERN ]] && [[ "$BRANCH" != "main" ]] && [[ "$BRANCH" != "develop" ]]; then
  echo "Branch name '$BRANCH' does not match pattern: $PATTERN"
  exit 1
fi

Best Practices

Common Pitfalls

| Pitfall | Fix |

|---------|-----|

| Hooks not running for teammates | Ensure "prepare": "husky" in package.json |

| Pre-commit is too slow | Use lint-staged, not full-project lint |

| tsc fails on unrelated files | That is correct -- type errors anywhere break the build |

| Hooks fail in CI environment | CI should set HUSKY=0 to skip hooks (it runs its own checks) |

| Forgot to run pnpm install | Husky installs via the prepare script automatically |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.