Enforcing Conventional Commits
- Create a global hooks directory
mkdir -p ~/.git-hooks - Save the hook script
# Create the commit-msg hook file cat > ~/.git-hooks/commit-msg << 'EOF' #!/bin/bash # Conventional commit pattern conventional_pattern='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?: .{1,50}' # Read commit message commit_message=$(cat "$1") # Validate format if ! echo "$commit_message" | grep -qE "$conventional_pattern"; then echo "" echo "❌ Commit message does not follow conventional commit format!" echo "" echo "Required format: <type>[optional scope]: <description>" echo "" echo "Valid types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert" echo "" echo "Examples:" echo " feat: add user authentication" echo " fix(auth): resolve login issue" echo " docs: update README" echo "" echo "Your message: '$commit_message'" echo "" exit 1 fi echo "✅ Commit message follows conventional format" exit 0 EOF - Make it executable
chmod +x ~/.git-hooks/commit-msg - Configure git to use this directory globally
git config --global core.hooksPath ~/.git-hooksNow all your git repositories will use this hook. Test it with a new commit:
# This should fail
git commit -m "bad commit message"
# This should succeed
git commit -m "feat: add new feature"
Note: Claude was used to generate the conventional-commit script.