Skip to content

Instantly share code, notes, and snippets.

@joshgachnang
Created February 7, 2026 18:18
Show Gist options
  • Select an option

  • Save joshgachnang/ea829b0a1c6f2c418a2cf58d13f07ff5 to your computer and use it in GitHub Desktop.

Select an option

Save joshgachnang/ea829b0a1c6f2c418a2cf58d13f07ff5 to your computer and use it in GitHub Desktop.
# Claude Worktree
# Create a worktree for a Claude session
cw() {
# Require branch name argument
if [ -z "$1" ]; then
echo "Error: Branch name required"
echo "Usage: cw <branch-name>"
return 1
fi
git fetch
local branch_name="$1"
local worktree_dir="$HOME/.claude-worktrees/$(basename $(git rev-parse --show-toplevel))/$branch_name"
# Check if worktree directory already exists
if [ -d "$worktree_dir" ]; then
echo "Worktree already exists at: $worktree_dir"
echo -n "Delete existing worktree and recreate? [y/N] "
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
echo "Removing existing worktree..."
git worktree remove "$worktree_dir" --force
else
echo "Aborted."
return 1
fi
fi
# Check if branch exists locally
if git rev-parse --verify "$branch_name" &>/dev/null; then
echo "Found local branch '$branch_name'"
echo -n "Delete existing branch and create fresh from base? [y/N] "
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
echo "Deleting existing branch..."
git branch -D "$branch_name"
else
echo "Using existing branch as base"
echo "Creating worktree: $worktree_dir"
# Create worktree from existing local branch
mkdir -p "$(dirname "$worktree_dir")"
git worktree add "$worktree_dir" "$branch_name"
# Set tmux window title and start Claude in the worktree
if [ -n "$TMUX" ]; then
tmux rename-window "$branch_name"
fi
cd "$worktree_dir" && claude
return 0
fi
fi
# Check if branch exists on origin (only if we didn't keep local branch above)
if git ls-remote --heads origin "$branch_name" | grep -q "$branch_name"; then
echo "Found branch '$branch_name' on origin, checking it out"
echo "Creating worktree: $worktree_dir"
# Fetch the branch from origin and create worktree
mkdir -p "$(dirname "$worktree_dir")"
git worktree add "$worktree_dir" -b "$branch_name" "origin/$branch_name"
else
# Branch doesn't exist anywhere, create new one from master/main
local base_branch
if git rev-parse --verify master &>/dev/null; then
base_branch="master"
elif git rev-parse --verify main &>/dev/null; then
base_branch="main"
else
echo "Error: Neither 'master' nor 'main' branch found"
return 1
fi
echo "Branch '$branch_name' not found, creating new branch from $base_branch"
echo "Creating worktree: $worktree_dir"
# Create worktree with new branch
mkdir -p "$(dirname "$worktree_dir")"
git worktree add -b "$branch_name" "$worktree_dir" "$base_branch"
fi
# Set tmux window title and start Claude in the worktree
if [ -n "$TMUX" ]; then
tmux rename-window "$branch_name"
fi
cd "$worktree_dir" && claude
}
# Clean up a worktree when done
cw-done() {
local current_dir="$(pwd)"
# Check if we're in a worktree
if ! git rev-parse --git-common-dir &>/dev/null; then
echo "Not in a git repository"
return 1
fi
local branch_name="$(git branch --show-current)"
local worktree_dir="$(git rev-parse --show-toplevel)"
# Go back to main repo
cd "$(git rev-parse --git-common-dir)/.."
# Ask what to do
echo "Current branch: $branch_name"
echo "Worktree: $worktree_dir"
echo ""
echo "What would you like to do?"
echo "1) Push branch and remove worktree"
echo "2) Just remove worktree (keep branch)"
echo "3) Cancel"
echo -n "Choice: "
read choice
case $choice in
1)
echo "Pushing $branch_name..."
git -C "$worktree_dir" push -u origin "$branch_name"
git worktree remove "$worktree_dir"
echo "Done! Create PR at: $(git remote get-url origin | sed 's/\.git$//')/compare/$branch_name"
;;
2)
git worktree remove "$worktree_dir"
echo "Worktree removed. Branch '$branch_name' still exists."
;;
*)
echo "Cancelled"
cd "$current_dir"
;;
esac
}
# List all Claude worktrees
cw-list() {
git worktree list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment