You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Claude Code Worktree Safety Hooks — 3 hooks to protect against worktree deletion, cross-tree destruction, and unmerged commit loss
gist-addition.md
Scheduled tasks can lose context and create the same PR repeatedly. One user reported 38 duplicate PRs created in a single session.
COMMAND=$(cat | jq -r '.tool_input.command // empty'2>/dev/null)
[ -z"$COMMAND" ] &&exit 0
echo"$COMMAND"| grep -qE '\bgh\s+pr\s+create\b'||exit 0
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
[ -z"$BRANCH" ] &&exit 0
EXISTING=$(gh pr list --head "$BRANCH" --state open --json number,title --jq '.[0].number'2>/dev/null)if [ -n"$EXISTING" ];then
TITLE=$(gh pr list --head "$BRANCH" --state open --json title --jq '.[0].title'2>/dev/null)echo"BLOCKED: An open PR already exists for branch '$BRANCH'.">&2echo" PR #$EXISTING: $TITLE">&2exit 2
fiexit 0
worktree-safety-hooks.md
Worktree issues are increasing rapidly in Claude Code (April 2026).
The main risks: one session deleting another's worktree, destructive git operations crossing worktree boundaries, and unmerged commits being silently lost.
These 3 hooks protect against the most common worktree incidents.
Prevents one session from deleting a worktree that another session is using.
Or install all 654+ safety hooks at once: npx cc-safe-setup
These worktree issues often cause retry loops that burn tokens fast.
For a systematic approach to reducing token consumption (CLAUDE.md optimization, context management, 9 token-saving hooks):
→ Token Book — Claude Codeのトークン消費を半分にする (¥2,500 / 10 chapters / 800h real data)
Part of cc-safe-setup — 655 safety hooks for Claude Code