chore: add Husky hooks to prevent Claude Code from bad git practices (#10937)

- Adds two Husky hooks to prevent Claude Code from bad git practices:
  - \`prepare-commit-msg\`: blocks \`git commit --amend\` (detected via the \`commit\` source argument)
  - \`pre-commit\`: blocks committing directly to \`main\`
- Both hooks detect Claude Code sessions via the \`CLAUDECODE\` environment variable, so regular users are unaffected
- Error messages explicitly tell Claude what to do instead
This commit is contained in:
Zoltan Kochan
2026-03-11 20:28:25 +01:00
committed by GitHub
parent 178f2210e3
commit edbe1bcbf9
2 changed files with 15 additions and 0 deletions

7
.husky/pre-commit Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/sh
# Prevent Claude Code from committing directly on the main branch.
if [ -n "$CLAUDECODE" ] && [ "$(git rev-parse --abbrev-ref HEAD)" = "main" ]; then
echo "error: Committing directly to the main branch is not allowed. Create a new branch and open a pull request instead." >&2
exit 1
fi

8
.husky/prepare-commit-msg Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/sh
# Prevent Claude Code from amending commits.
# The prepare-commit-msg hook receives "commit" as $2 when --amend is used.
if [ "$2" = "commit" ] && [ -n "$CLAUDECODE" ]; then
echo "error: Amending commits is not allowed. Create a new commit instead." >&2
exit 1
fi