- Add Section 9.12: Git Best Practices & Workflows (~400 lines) - Commit message best practices with Conventional Commits - Git amend workflow with safety rules - Branch management and naming conventions - Rewind vs Revert decision tree - Comprehensive git worktrees documentation - Add Section 9.13: Cost Optimization Strategies (~350 lines) - Model selection matrix (Haiku/Sonnet/Opus) - OpusPlan mode for efficient workflows - Token-saving techniques and cost tracking - ROI calculations and economic workflows - Add examples/commands/git-worktree.md slash command template - Update CHANGELOG.md with v2.0.0 additions - Update version metadata to 2.0 across documentation Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
3.1 KiB
3.1 KiB
Git Worktree Setup
Create isolated git worktrees for feature development without switching branches.
Core principle: Systematic directory selection + safety verification = reliable isolation.
Process
- Check Existing Directories:
.worktrees/orworktrees/ - Verify .gitignore: Ensure worktree dir is ignored
- Create Worktree:
git worktree add - Install Dependencies: Auto-detect package manager
- Run Baseline Tests: Verify clean state
- Report Location: Confirm ready
Directory Selection
Priority Order
# 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 exists
If both exist: .worktrees/ wins.
Safety Verification
For project-local directories:
# Check if directory in .gitignore
grep -q "^\.worktrees/$" .gitignore || grep -q "^worktrees/$" .gitignore
If NOT in .gitignore:
- Add line to .gitignore
- Commit the change
- Proceed with worktree creation
Why critical: Prevents accidentally committing worktree contents.
Creation Steps
# 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
Auto-Detect Setup
# 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
Baseline Verification
# Run tests to verify clean state
pnpm test # Node.js
cargo test # Rust
pytest # Python
go test ./... # Go
If tests fail: Report failures, ask whether to proceed. If tests pass: Report ready.
Final Report
Worktree ready at <full-path>
Tests passing (<N> tests, 0 failures)
Ready to implement <feature-name>
Quick Reference
| 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 |
| Tests fail | Report + ask to proceed |
Common Mistakes
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
Cleanup (After Work Complete)
# Remove worktree
git worktree remove .worktrees/$BRANCH_NAME
# Or force if uncommitted changes
git worktree remove --force .worktrees/$BRANCH_NAME
# Prune stale worktrees
git worktree prune
Usage
/git-worktree feature/auth
/git-worktree fix/session-bug
Branch name: $ARGUMENTS