Back to Hooks

Commit Message Linter

PreToolUse

Validates that commit messages follow conventional commit format before allowing commits

gitcommitlintingconventional-commits

Hook Script

#!/bin/bash
# Commit Message Linter Hook
# Validates conventional commit format on git commit commands

COMMAND="$1"

# Only check git commit commands
if ! echo "$COMMAND" | grep -q "git commit"; then
  exit 0
fi

# Extract the commit message from -m flag
MSG=$(echo "$COMMAND" | grep -oP '(?<=-m\s["\''])[^"\'']+')

if [ -z "$MSG" ]; then
  exit 0
fi

# Validate conventional commit format: type(scope): description
PATTERN="^(feat|fix|docs|style|refactor|test|chore|perf|ci|build|revert)(\(.+\))?!?:\s.+"

if ! echo "$MSG" | grep -qE "$PATTERN"; then
  echo "INVALID COMMIT MESSAGE FORMAT"
  echo ""
  echo "Expected: <type>(<scope>): <description>"
  echo "Types: feat, fix, docs, style, refactor, test, chore, perf, ci, build, revert"
  echo ""
  echo "Examples:"
  echo "  feat(auth): add password reset flow"
  echo "  fix: resolve null pointer in parser"
  echo "  docs(readme): update installation steps"
  exit 1
fi

exit 0

Settings Configuration

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "command": "./hooks/commit-lint.sh"
      }
    ]
  }
}

How to use

  1. Create a hooks directory in your project: mkdir hooks
  2. Save the hook script as hooks/commit-lint.sh
  3. Make it executable: chmod +x hooks/commit-lint.sh
  4. Add the configuration to your Claude Code settings
  5. Restart Claude Code to apply changes