create-worktree
Create Worktree with Environment Setup
When this command is invoked with /create-worktree <branch-name> [base-branch] [path=<folder>], execute the following steps:
Step 1: Parse Arguments
Extract the branch name, optional base branch, and optional path override from the command arguments.
Derive the worktree folder name:
- If an explicit
path=<folder>argument was provided, use that. - Otherwise, use the last segment of the branch name (the part after the final
/).autoresearch/multisector-mar22→multisector-mar22feature/auth→authfeature-auth→feature-auth(no slash, use as-is)
The worktree will be created at ../{folder}. The git branch is still created as {branch-name} (preserving the full hierarchical name).
Step 2: Create Git Worktree
Execute the git worktree add command:
# If base-branch provided:
git worktree add ../{folder} -b {branch-name} {base-branch}
# If no base-branch provided:
git worktree add ../{folder} -b {branch-name}
The worktree will be created in the parent directory at ../{folder}.
Step 3: Identify Source and Target Paths
- Source: Current working directory (the original repo)
- Target:
../{folder}(the new worktree)
Step 4: Copy .env Files
Use Bash to check for and copy .env files (gitignored files are not found by Glob):
Check common .env file locations explicitly:
- Top-level:
.env - Frontend:
frontend/.env,frontend/.env.local - Backend:
backend/.env,backend/.env.local
For each location, use test -f to check if the file exists, then copy it:
# Check and copy .env files
for env_file in .env backend/.env frontend/.env backend/.env.local frontend/.env.local; do
if [ -f "$env_file" ]; then
target_dir=$(dirname "../{folder}/$env_file")
mkdir -p "$target_dir"
cp "$env_file" "../{folder}/$env_file"
echo "Copied $env_file"
fi
done
This approach:
- Checks each known .env location explicitly with
test -f - Creates target directory if needed with
mkdir -p - Copies only files that exist
- Works with gitignored files that Glob cannot see
Step 5: Install Python Dependencies
Check which Python package manager the project uses, then install dependencies in the worktree:
If project uses uv (has pyproject.toml + uv.lock):
- Run
uv sync— recreates.venvfrom the lockfile, no copying needed
# uv projects — run in the worktree backend directory
cd ../{folder}/backend
uv sync
If project uses pip (has requirements.txt):
- Copy the existing venv, then run pip install
# Copy venv (all platforms)
cp -r backend/venv ../{folder}/backend/venv
# Install dependencies
# Windows
cd ..\{folder}\backend && venv\Scripts\python -m pip install -r requirements.txt
# Unix/Mac
cd ../{folder}/backend && venv/bin/python -m pip install -r requirements.txt
Check both top-level and backend/ for pyproject.toml/uv.lock or requirements.txt.
Step 7: Install npm Dependencies
Use Glob to find all package.json files in:
- Top-level:
package.json - Frontend:
frontend/package.json - Backend:
backend/package.json
For each package.json found:
- Navigate to the target worktree directory containing the package.json
- Run npm install
# Windows
cd ..\{folder}\frontend
npm install
# Unix/Mac
cd ../{folder}/frontend
npm install
Important: Install npm dependencies in ALL directories that have a package.json file (top-level, frontend/, backend/, or any other subdirectories).
Step 8: Report Completion
Provide a summary to the user:
- Worktree created at:
../{folder} - .env files copied: [list]
- Python dependencies installed in: [list] (method: uv sync / pip install)
- npm dependencies installed in: [list]
Platform Detection
Detect the platform using:
# Check if Windows (look for .bat or .ps1 files, or check OS)
uname -s # Returns "MINGW64_NT" or similar on Windows Git Bash
Use appropriate commands (copy vs cp, robocopy vs cp -r, Scripts vs bin) based on platform.
Error Handling
- If git worktree add fails, stop and report error
- If .env copy fails, warn but continue
- If uv sync / pip install fails, warn but continue
- If npm install fails, warn but continue
- Always report which steps succeeded and which failed
Example Execution
User runs: /create-worktree feature-auth main
Expected output (branch has no /, so folder = branch name):
Creating worktree 'feature-auth' based on 'main'...
✓ Worktree created at ../feature-auth
Copying .env files...
✓ Copied backend/.env
✓ Copied frontend/.env
Installing Python dependencies...
✓ uv sync in backend/ (recreated .venv from uv.lock)
Installing npm dependencies...
✓ Installed frontend/package.json (127 packages)
✓ Installed backend/package.json (43 packages)
Worktree setup complete! You can now work in ../feature-auth
User runs: /create-worktree autoresearch/multisector-mar22 master
Expected output (last segment of branch name used as folder):
Creating worktree 'autoresearch/multisector-mar22' based on 'master'...
✓ Worktree created at ../multisector-mar22
(branch: autoresearch/multisector-mar22, folder: multisector-mar22)
...
Worktree setup complete! You can now work in ../multisector-mar22