Create isolated git worktrees for feature development without switching branches.
Core principle: Systematic directory selection + safety verification = reliable isolation.
- Check Existing Directories:
.worktrees/orworktrees/ - Verify .gitignore: Ensure worktree dir is ignored
- Create Worktree:
git worktree add - Detect Database Provider: Check for DB branching capability
- Suggest Database Branch: Remind user with exact commands
- Install Dependencies: Auto-detect package manager
- Run Baseline Tests: Verify clean state
- Report Location: Confirm ready
# 1. Check existing directories
ls -d .worktrees 2>/dev/null # Preferred (hidden)
ls -d worktrees 2>/dev/null # Alternative
# 2. Check CLAUDE.md for preference
grep -i "worktree.*director" CLAUDE.md 2>/dev/null
# 3. Ask user if neither existsIf both exist: .worktrees/ wins.
For project-local directories:
# Check if directory in .gitignore
grep -q "^\.worktrees/$" .gitignore || grep -q "^worktrees/$" .gitignoreIf NOT in .gitignore:
- Add line to .gitignore
- Commit the change
- Proceed with worktree creation
Why critical: Prevents accidentally committing worktree contents.
# 1. Detect project name
project=$(basename "$(git rev-parse --show-toplevel)")
# 2. Create worktree with new branch
git worktree add .worktrees/$BRANCH_NAME -b $BRANCH_NAME
# 3. Navigate
cd .worktrees/$BRANCH_NAME# Node.js
if [ -f package.json ]; then pnpm install; fi
# Rust
if [ -f Cargo.toml ]; then cargo build; fi
# Python
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f pyproject.toml ]; then poetry install; fi
# Go
if [ -f go.mod ]; then go mod download; fi# Run tests to verify clean state
pnpm test # Node.js
cargo test # Rust
pytest # Python
go test ./... # GoIf tests fail: Report failures, ask whether to proceed. If tests pass: Report ready.
Worktree ready at <full-path>
Tests passing (<N> tests, 0 failures)
Ready to implement <feature-name>
After worktree creation, command detects database provider and suggests isolation.
| Provider | Suggested Command |
|---|---|
| Neon | neonctl branches create --name <branch> --parent main |
| PlanetScale | pscale branch create <db> <branch> |
| Local Postgres | psql -c "CREATE SCHEMA <schema>;" |
| Other | Manual setup or shared DB |
Example output:
✅ Worktree created at .worktrees/feature-auth
💡 DB Isolation: neonctl branches create --name feature-auth --parent main
Then update .env with new DATABASE_URL
Full guide: ../workflows/database-branch-setup.md
Critical for environment variables:
# .worktreeinclude (at project root)
.env
.env.local
.env.development
**/.claude/settings.local.jsonWhy: Without this, .env files won't be copied to worktrees → Claude sessions fail.
| Scenario | Create Branch? |
|---|---|
| Schema migrations | ✅ Yes |
| Data model refactoring | ✅ Yes |
| Bug fix (no schema change) | ❌ No |
| Performance experiments | ✅ Yes |
See: Database Branch Setup Guide for complete workflows.
| Situation | Action |
|---|---|
.worktrees/ exists |
Use it (verify .gitignore) |
worktrees/ exists |
Use it (verify .gitignore) |
| Both exist | Use .worktrees/ |
| Neither exists | Check CLAUDE.md → Ask user |
| Not in .gitignore | Add + commit immediately |
| Neon detected | Suggest neonctl branches create |
| PlanetScale detected | Suggest pscale branch create |
| No .worktreeinclude | Create with .env pattern |
| Tests fail | Report + ask to proceed |
Skipping .gitignore verification
- Worktree contents get tracked, pollute git status
Assuming directory location
- Follow priority: existing > CLAUDE.md > ask
Proceeding with failing tests
- Can't distinguish new bugs from pre-existing
Not copying .env to worktree
- Symptom: Claude fails with "DATABASE_URL not found"
- Fix: Add
.envto.worktreeinclude
Using shared database for schema changes
- Symptom: Migration conflicts, broken dev environment
- Fix: Create database branch before modifying schema
# 1. Remove git worktree
git worktree remove .worktrees/$BRANCH_NAME
# Or force if uncommitted changes
git worktree remove --force .worktrees/$BRANCH_NAME
# 2. If you created a database branch, delete it
# Neon:
neonctl branches delete $BRANCH_NAME
# PlanetScale:
pscale branch delete <database-name> $BRANCH_NAME
# Local schema:
psql $DATABASE_URL -c "DROP SCHEMA ${BRANCH_NAME/\//_} CASCADE;"
# 3. Prune stale references
git worktree prune/git-worktree feature/auth
/git-worktree fix/session-bug
Branch name: $ARGUMENTS