Manage git worktrees for isolated development. Use when user asks to create isolated workspaces, work on multiple branches simultaneously, set up parallel development environments, or clean up old worktrees.
Creates and manages isolated git worktrees for parallel development. Use when user asks to work on multiple branches simultaneously, set up isolated workspaces, or clean up old worktrees.
/plugin marketplace add cruzanstx/daplug/plugin install daplug@cruzanstxThis skill is limited to using the following tools:
Create, list, and manage git worktrees for isolated development environments.
Worktree directory location is configurable per-project via CLAUDE.md.
<daplug_config>
worktree_dir: /absolute/path/to/worktrees
# OR
worktree_dir: .worktrees/
</daplug_config>
This is the canonical way to resolve the worktree directory. All operations should use this lookup.
REPO_ROOT=$(git rev-parse --show-toplevel)
PLUGIN_ROOT=$(jq -r '.plugins."daplug@cruzanstx"[0].installPath' ~/.claude/plugins/installed_plugins.json)
CONFIG_READER="$PLUGIN_ROOT/skills/config-reader/scripts/config.py"
# 1. Check for worktree_dir in CLAUDE.md
CONFIGURED_DIR=$(python3 "$CONFIG_READER" get worktree_dir --repo-root "$REPO_ROOT")
if [ -n "$CONFIGURED_DIR" ]; then
# 2a. Expand relative paths (starts with . or no leading /)
if [[ "$CONFIGURED_DIR" == .* ]] || [[ "$CONFIGURED_DIR" != /* ]]; then
WORKTREES_DIR=$(realpath "${REPO_ROOT}/${CONFIGURED_DIR}")
else
WORKTREES_DIR="$CONFIGURED_DIR"
fi
else
# 2b. No config found - STOP and prompt user (see below)
echo "NO_CONFIG"
fi
IMPORTANT: If no config exists (CONFIGURED_DIR is empty), you MUST prompt the user before proceeding.
Do NOT silently fall back to a default. Use the "Configure Worktree Directory (Interactive)" section below to ask the user their preference and store it.
When no configuration exists and user needs to set one up:
Ask user for preference using AskUserQuestion:
Store the preference in CLAUDE.md:
REPO_ROOT=$(git rev-parse --show-toplevel)
PLUGIN_ROOT=$(jq -r '.plugins."daplug@cruzanstx"[0].installPath' ~/.claude/plugins/installed_plugins.json)
CONFIG_READER="$PLUGIN_ROOT/skills/config-reader/scripts/config.py"
# Add config to CLAUDE.md under <daplug_config>
python3 "$CONFIG_READER" set worktree_dir "${CHOSEN_PATH}" --scope project
# Add to .gitignore if not present
if [ -f "${REPO_ROOT}/.gitignore" ]; then
if ! grep -qxF "${CONFIGURED_DIR}" "${REPO_ROOT}/.gitignore" && \
! grep -qxF "${CONFIGURED_DIR}/" "${REPO_ROOT}/.gitignore"; then
echo "" >> "${REPO_ROOT}/.gitignore"
echo "# Worktrees directory (local development)" >> "${REPO_ROOT}/.gitignore"
echo "${CONFIGURED_DIR}/" >> "${REPO_ROOT}/.gitignore"
fi
else
echo "# Worktrees directory (local development)" > "${REPO_ROOT}/.gitignore"
echo "${CONFIGURED_DIR}/" >> "${REPO_ROOT}/.gitignore"
fi
# Add to .dockerignore if not present
if [ -f "${REPO_ROOT}/.dockerignore" ]; then
if ! grep -qxF "${CONFIGURED_DIR}" "${REPO_ROOT}/.dockerignore" && \
! grep -qxF "${CONFIGURED_DIR}/" "${REPO_ROOT}/.dockerignore"; then
echo "" >> "${REPO_ROOT}/.dockerignore"
echo "# Worktrees directory" >> "${REPO_ROOT}/.dockerignore"
echo "${CONFIGURED_DIR}/" >> "${REPO_ROOT}/.dockerignore"
fi
fi
Read(/absolute/path/**), Edit(/absolute/path/**), Write(/absolute/path/**)additionalDirectories# Get repo info
REPO_ROOT=$(git rev-parse --show-toplevel)
REPO_NAME=$(basename "$REPO_ROOT")
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
PLUGIN_ROOT=$(jq -r '.plugins."daplug@cruzanstx"[0].installPath' ~/.claude/plugins/installed_plugins.json)
CONFIG_READER="$PLUGIN_ROOT/skills/config-reader/scripts/config.py"
# Get configured worktree directory (see Configuration section)
CONFIGURED_DIR=$(python3 "$CONFIG_READER" get worktree_dir --repo-root "$REPO_ROOT")
if [ -n "$CONFIGURED_DIR" ]; then
if [[ "$CONFIGURED_DIR" == .* ]] || [[ "$CONFIGURED_DIR" != /* ]]; then
WORKTREES_DIR=$(realpath "${REPO_ROOT}/${CONFIGURED_DIR}")
else
WORKTREES_DIR="$CONFIGURED_DIR"
fi
else
WORKTREES_DIR=$(realpath "${REPO_ROOT}/../worktrees")
fi
# Generate unique identifier
RUN_ID="$(date +%Y%m%d-%H%M%S)"
# Create worktree with new branch
BRANCH_NAME="feature/${PURPOSE}-${RUN_ID}"
WORKTREE_PATH="${WORKTREES_DIR}/${REPO_NAME}-${PURPOSE}-${RUN_ID}"
mkdir -p "$WORKTREES_DIR"
git worktree add -b "$BRANCH_NAME" "$WORKTREE_PATH" "$CURRENT_BRANCH"
echo "Created worktree:"
echo " Path: $WORKTREE_PATH"
echo " Branch: $BRANCH_NAME"
echo " Based on: $CURRENT_BRANCH"
git worktree list
# For each worktree, show branch and commit count
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
for worktree in $(git worktree list --porcelain | grep "^worktree" | cut -d' ' -f2); do
branch=$(git -C "$worktree" rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ "$branch" != "$CURRENT_BRANCH" ]; then
commits=$(git rev-list --count "$CURRENT_BRANCH".."$branch" 2>/dev/null || echo "0")
echo "$worktree ($branch): $commits commits ahead"
fi
done
CRITICAL: Ensure your CWD is NOT the worktree you're deleting!
If your shell's current working directory is the worktree being deleted, the shell will break and all subsequent bash commands will fail with "No such file or directory".
# FIRST: Ensure you're in the main repo, not the worktree
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || REPO_ROOT="/path/to/main/repo"
cd "$REPO_ROOT"
# Verify we're not in a worktree path
if [[ "$(pwd)" == *"/worktrees/"* ]]; then
echo "ERROR: Cannot remove worktree while inside it!"
exit 1
fi
# Remove specific worktree by path (use subshell for safety)
(cd "$REPO_ROOT" && git worktree remove /path/to/worktree)
# Or force remove if there are changes
(cd "$REPO_ROOT" && git worktree remove --force /path/to/worktree)
# Clean up stale references
git worktree prune
# Delete the branch after worktree is removed
git branch -D branch-name
# Get configured worktree directory (see Configuration section)
REPO_ROOT=$(git rev-parse --show-toplevel)
PLUGIN_ROOT=$(jq -r '.plugins."daplug@cruzanstx"[0].installPath' ~/.claude/plugins/installed_plugins.json)
CONFIG_READER="$PLUGIN_ROOT/skills/config-reader/scripts/config.py"
CONFIGURED_DIR=$(python3 "$CONFIG_READER" get worktree_dir --repo-root "$REPO_ROOT")
if [ -n "$CONFIGURED_DIR" ]; then
if [[ "$CONFIGURED_DIR" == .* ]] || [[ "$CONFIGURED_DIR" != /* ]]; then
WORKTREES_DIR=$(realpath "${REPO_ROOT}/${CONFIGURED_DIR}")
else
WORKTREES_DIR="$CONFIGURED_DIR"
fi
else
WORKTREES_DIR=$(realpath "${REPO_ROOT}/../worktrees")
fi
# Find worktrees older than 7 days
find "$WORKTREES_DIR" -maxdepth 1 -type d -mtime +7 | while read dir; do
echo "Removing old worktree: $dir"
git worktree remove "$dir" 2>/dev/null || true
done
git worktree prune
WORKTREE_PATH="/path/to/worktree"
BRANCH_NAME=$(git -C "$WORKTREE_PATH" rev-parse --abbrev-ref HEAD)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# First, remove the worktree
git worktree remove "$WORKTREE_PATH"
# Then merge the branch
git merge --no-ff "$BRANCH_NAME" -m "Merge $BRANCH_NAME"
# Optionally delete the branch
git branch -d "$BRANCH_NAME"
Worktrees require file permissions for the worktrees directory. Check/add to ~/.claude/settings.json:
{
"permissions": {
"allow": [
"Read(/path/to/worktrees/**)",
"Edit(/path/to/worktrees/**)",
"Write(/path/to/worktrees/**)"
],
"additionalDirectories": [
"/path/to/worktrees"
]
}
}
worktree_dir under <daplug_config> in CLAUDE.md for consistency
../worktrees/) - default, avoids git conflicts.worktrees/) - self-contained, must be gitignoredgit worktree prune to clean stale references"Branch already checked out"
git worktree list # Find conflicting worktree
git worktree remove /path/to/conflicting
"Dirty working directory"
git stash
# Create worktree...
git stash pop
Stale worktree references
git worktree prune
Shell breaks after deleting worktree (all bash commands fail)
This happens when your CWD was the worktree you deleted. The shell can't resolve . anymore.
Symptoms:
echo test failSolutions:
cd to repo root before cleanup:
cd "$REPO_ROOT" # Do this BEFORE removing worktree
git worktree remove /path/to/worktree
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.