| name | worktree |
| description | 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. |
| allowed-tools | ["Bash(git:*)","Bash(git worktree:*)","Bash(git branch:*)","Bash(mkdir:*)","Bash(find:*)","Bash(realpath:*)","Bash(jq:*)","Bash(python3:*)","Bash(REPO_ROOT=:*)","Bash(REPO_NAME=:*)","Bash(WORKTREE_PATH=:*)","Bash(WORKTREES_DIR=:*)","Bash(BRANCH_NAME=:*)","Bash(CONFIGURED_DIR=:*)","Bash(TIMESTAMP=:*)","Bash(\\:*)","Read","Edit","Write","Glob","Grep"] |
Git Worktree Management
Create, list, and manage git worktrees for isolated development environments.
When to Use This Skill
- User asks to "create an isolated workspace" or "work on multiple branches"
- User wants to "set up a worktree" or "parallel development"
- User asks to "list worktrees" or "clean up worktrees"
- Before running parallel tasks that might conflict
Configuration
Worktree directory location is configurable per-project via CLAUDE.md.
Config Format (in CLAUDE.md)
<daplug_config>
worktree_dir: /absolute/path/to/worktrees
# OR
worktree_dir: .worktrees/
</daplug_config>
Get Worktree Directory
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"
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
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.
Configure Worktree Directory (Interactive)
When no configuration exists and user needs to set one up:
-
Ask user for preference using AskUserQuestion:
- "Sibling directory (../worktrees/)" - default, outside repo
- "Inside project (.worktrees/)" - local, will be gitignored
- "Custom path" - let them specify absolute path
-
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"
python3 "$CONFIG_READER" set worktree_dir "${CHOSEN_PATH}" --scope project
- If path is inside project (relative path chosen), ensure gitignore:
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
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
- Update Claude permissions for the resolved absolute path:
- Add
Read(/absolute/path/**), Edit(/absolute/path/**), Write(/absolute/path/**)
- Add to
additionalDirectories
Core Operations
Create a Worktree
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"
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
RUN_ID="$(date +%Y%m%d-%H%M%S)"
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"
List Worktrees
git worktree list
Check Worktree Status
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
Remove a Worktree
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".
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || REPO_ROOT="/path/to/main/repo"
cd "$REPO_ROOT"
if [[ "$(pwd)" == *"/worktrees/"* ]]; then
echo "ERROR: Cannot remove worktree while inside it!"
exit 1
fi
(cd "$REPO_ROOT" && git worktree remove /path/to/worktree)
(cd "$REPO_ROOT" && git worktree remove --force /path/to/worktree)
git worktree prune
git branch -D branch-name
Cleanup Old Worktrees
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_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
Merge Worktree Branch
WORKTREE_PATH="/path/to/worktree"
BRANCH_NAME=$(git -C "$WORKTREE_PATH" rev-parse --abbrev-ref HEAD)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
git worktree remove "$WORKTREE_PATH"
git merge --no-ff "$BRANCH_NAME" -m "Merge $BRANCH_NAME"
git branch -d "$BRANCH_NAME"
Permission Setup
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"
]
}
}
Best Practices
- Use RUN_ID timestamps - Prevents naming collisions on reruns
- Track what you create - Store paths/branches in arrays for cleanup
- Configure worktree location - Set
worktree_dir under <daplug_config> in CLAUDE.md for consistency
- Sibling directory (
../worktrees/) - default, avoids git conflicts
- Inside project (
.worktrees/) - self-contained, must be gitignored
- Prune regularly - Run
git worktree prune to clean stale references
- Check for dirty state - Ensure no uncommitted changes before creating worktrees
Troubleshooting
"Branch already checked out"
git worktree list
git worktree remove /path/to/conflicting
"Dirty working directory"
git stash
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:
- All bash commands return exit code 1
- Error: "fatal: Unable to read current working directory: No such file or directory"
- Even simple commands like
echo test fail
Solutions:
- Start a new terminal/session - cleanest fix
- Use file tools instead of bash - Read/Write/Glob still work
- Prevention: Always
cd to repo root before cleanup:
cd "$REPO_ROOT"
git worktree remove /path/to/worktree