Skip to content

Instantly share code, notes, and snippets.

@MatthewDailey
Last active July 2, 2025 17:55
Show Gist options
  • Select an option

  • Save MatthewDailey/399a32818f70f30aa5adcc68df4179fb to your computer and use it in GitHub Desktop.

Select an option

Save MatthewDailey/399a32818f70f30aa5adcc68df4179fb to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <branch-name>"
exit 1
fi
# Get editor command, default to claude
EDITOR_CMD=${2:-claude}
BRANCH_NAME=$1
MAIN_DIR=$(pwd)
REPO_NAME=$(basename "$MAIN_DIR")
PARENT_DIR=$(dirname "$MAIN_DIR")
WORKTREE_DIR="${PARENT_DIR}/${REPO_NAME}-${BRANCH_NAME}-worktree"
# Check if branch exists
if git show-ref --verify --quiet refs/heads/$BRANCH_NAME; then
echo "βœ… Branch '$BRANCH_NAME' exists."
else
echo "🌱 Creating new branch '$BRANCH_NAME' from current HEAD..."
git branch "$BRANCH_NAME"
fi
# Check if worktree already exists
if [ -d "$WORKTREE_DIR" ]; then
echo "⚠️ Worktree directory already exists at $WORKTREE_DIR"
echo "Remove it with: git worktree remove \"$WORKTREE_DIR\""
exit 1
fi
# Create worktree
echo "πŸ“ Adding worktree for branch '$BRANCH_NAME' at $WORKTREE_DIR..."
git worktree add "$WORKTREE_DIR" "$BRANCH_NAME"
cd $WORKTREE_DIR
# unset keys
unset GITHUB_TOKEN
unset ANTHROPIC_API_KEY
# Launch dev script in background
echo "πŸš€ Launching claude in $WORKTREE_DIR..."
if [ -f "agent-setup.sh" ]; then
echo "πŸ”§ Running agent setup script..."
chmod +x agent-setup.sh
./agent-setup.sh
fi
$EDITOR_CMD .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment