Skip to content

Instantly share code, notes, and snippets.

@CodeOfficer
Forked from TheBaconWizard/README.md
Created January 10, 2026 14:02
Show Gist options
  • Select an option

  • Save CodeOfficer/38a000588483e3575c7df9164599610a to your computer and use it in GitHub Desktop.

Select an option

Save CodeOfficer/38a000588483e3575c7df9164599610a to your computer and use it in GitHub Desktop.
claude-work-tree: Quickly spin up isolated Claude Code sessions using git worktrees

claude-work-tree

Quickly spin up isolated Claude Code sessions using git worktrees.

What it does

  • Creates a git worktree for your feature branch
  • Opens Claude Code with --dangerously-skip-permissions in that worktree
  • Tab completion for existing worktrees and branches

Installation

  1. Download the script to your PATH:

    mkdir -p ~/bin
    curl -o ~/bin/claude-work-tree https://gist.githubusercontent.com/TheBaconWizard/9883ce7255ae6c972b7be73016d27c8a/raw/claude-work-tree
    chmod +x ~/bin/claude-work-tree
  2. Add to your .zshrc:

    export PATH="$HOME/bin:$PATH"
    alias cwt="claude-work-tree"
    eval "$(claude-work-tree --completions)"
  3. Reload your shell:

    source ~/.zshrc

Usage

# Create new branch + worktree and open Claude
cwt feature-branch

# Open existing worktree
cwt existing-branch

# Tab completion shows available worktrees and branches
cwt <tab>

Worktrees are created at ../repo-name-worktrees/branch-name relative to your repo.

Recommended: Safety Net Plugin

This script enables --dangerously-skip-permissions for faster workflows. For protection against destructive commands, install claude-code-safety-net:

/plugin marketplace add kenryu42/cc-marketplace
/plugin install safety-net@cc-marketplace

The script will warn you if this plugin isn't installed.

#!/bin/bash
print_completions() {
cat << 'COMPLETIONS'
_cwt_completion() {
local repo_root repo_name worktree_dir
repo_root=$(git rev-parse --show-toplevel 2>/dev/null) || return
repo_name=$(basename "$repo_root")
worktree_dir="$(dirname "$repo_root")/${repo_name}-worktrees"
local -a worktrees branches
[[ -d "$worktree_dir" ]] && worktrees=($(ls "$worktree_dir" 2>/dev/null))
branches=($(git branch --format='%(refname:short)' 2>/dev/null))
_describe 'worktrees' worktrees
_describe 'branches' branches
}
compdef _cwt_completion cwt claude-work-tree
COMPLETIONS
}
find_claude() {
command -v claude 2>/dev/null && return
[[ -x "$HOME/.claude/local/claude" ]] && echo "$HOME/.claude/local/claude" && return
echo "Error: claude not found in PATH or ~/.claude/local/" >&2
exit 1
}
check_safety_net() {
[[ -d "$HOME/.cc-safety-net" ]] && return
echo "Warning: claude-code-safety-net plugin not detected."
echo "For added protection against destructive commands, install it:"
echo " /plugin marketplace add kenryu42/cc-marketplace"
echo " /plugin install safety-net@cc-marketplace"
echo ""
}
[[ "$1" == "--completions" ]] && print_completions && exit 0
check_safety_net
claude_bin=$(find_claude)
branch_name="$1"
[[ -z "$branch_name" ]] && echo "Usage: cwt <branch-name>" && exit 1
repo_root=$(git rev-parse --show-toplevel 2>/dev/null)
[[ -z "$repo_root" ]] && echo "Not in a git repository" && exit 1
repo_name=$(basename "$repo_root")
worktree_path="$(dirname "$repo_root")/${repo_name}-worktrees/${branch_name}"
if [[ -d "$worktree_path" ]]; then
"$claude_bin" --dangerously-skip-permissions "$worktree_path"
elif git show-ref --verify --quiet "refs/heads/${branch_name}"; then
git worktree add "$worktree_path" "$branch_name" && \
"$claude_bin" --dangerously-skip-permissions "$worktree_path"
else
git worktree add -b "$branch_name" "$worktree_path" && \
"$claude_bin" --dangerously-skip-permissions "$worktree_path"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment