I’ve been working on multiple tasks simultaneously using Claude Code and Git worktree. It’s great to be able to move multiple things at once, which reduces the waiting time. Just be careful, it’s easy to mix the tasks 😆.
This is useful when tackling a bunch of minor bugs. So I have my main objective in the window called claude-1
, and secondary tasks or refactors on the other windows. I often rename the windows to match the task/branch so I don’t get lost.
I started having one worktree per feature, but reusing an existing worktree multiple times is more effective, as some manual setup is often necessary.
This is a script that I created to create a “copy” of my repository to another folder, update database configs, seed the database and create a new tmux window for me to start working on a separate task.
#!/bin/bash
# Script to create a new git worktree with branch-specific database configuration
# Usage: ./create_worktree.sh <branch-name>
set -e
if [ $# -eq 0 ]; then
echo "Usage: $0 <branch-name>"
echo "Example: $0 feature/user-auth"
exit 1
fi
BRANCH_NAME="$1"
WORKTREE_DIR="../$BRANCH_NAME"
CURRENT_DIR=$(pwd)
# Check if .env exists in current directory
if [ ! -f ".env" ]; then
echo "Error: .env file not found in current directory"
exit 1
fi
echo "Creating worktree for branch '$BRANCH_NAME' in '$WORKTREE_DIR'..."
# Create the worktree
git worktree add "$WORKTREE_DIR" -b "$BRANCH_NAME"
echo "Copying .env file to new worktree..."
# Copy .env to the new worktree
cp .env "$WORKTREE_DIR/.env"
echo "Updating database URLs in new worktree..."
# Update the database URLs in the new worktree's .env file
# Replace development with development_<branch-name>
# Replace test with test_<branch-name>
# Handle branch names with slashes by replacing them with underscores
SAFE_BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9]/_/g')
sed -i.bak \
-e "s/development/development_${SAFE_BRANCH_NAME}/g" \
-e "s/test/test_${SAFE_BRANCH_NAME}/g" \
"$WORKTREE_DIR/.env"
# Remove the backup file
rm "$WORKTREE_DIR/.env.bak"
echo "Switching to new worktree and running setup..."
# Change to the new worktree directory and run bin/setup
cd "$WORKTREE_DIR"
bin/setup --skip-server
echo "✅ Worktree created and set up successfully!"
echo "📂 Location: $WORKTREE_DIR"
echo "🗄️ Development DB: development_${SAFE_BRANCH_NAME}"
echo "🧪 Test DB: test_${SAFE_BRANCH_NAME}"
echo ""
# Check if tmux is available and we're in a tmux session
if command -v tmux >/dev/null 2>&1 && [ -n "$TMUX" ]; then
echo "Creating new tmux window '$BRANCH_NAME'..."
tmux new-window -n "$BRANCH_NAME" -c "$WORKTREE_DIR"
echo "🪟 New tmux window created and switched to worktree directory"
else
echo "You can now switch to the new worktree:"
echo " cd $WORKTREE_DIR"
fi