home / skills / bbeierle12 / skill-mcp-claude / using-git-worktrees

using-git-worktrees skill

/skills/using-git-worktrees

This skill guides you to create isolated git worktrees for feature work, keeping main clean and speeding context switches.

npx playbooks add skill bbeierle12/skill-mcp-claude --skill using-git-worktrees

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
3.7 KB
---
name: using-git-worktrees
description: Use when starting new feature work to create isolated git worktrees with smart directory selection and safety verification. Keeps main branch clean while developing.
---

# Using Git Worktrees

## Core Principle

**Isolate feature work. Keep main clean.**

Git worktrees let you work on multiple branches simultaneously in separate directories.

## When to Use

- Starting a new feature
- Working on a bugfix while main development continues
- Need to context-switch without stashing
- Want clean separation between work streams

## Setup Process

### Step 1: Check for Existing Worktree Directory

```bash
# Check in priority order
ls -d .worktrees 2>/dev/null    # Preferred (hidden)
ls -d worktrees 2>/dev/null     # Alternative
```

If found: Use that directory.
If both exist: `.worktrees` wins.

### Step 2: Check CLAUDE.md for Preferences

```bash
grep -i "worktree.*director" CLAUDE.md 2>/dev/null
```

If preference specified: Use it without asking.

### Step 3: If No Directory Exists

Ask the user:
```
No worktree directory found. Where should I create worktrees?
1. .worktrees/ (project-local, hidden)
2. worktrees/ (project-local, visible)
3. ~/.config/superpowers/worktrees/<project-name>/ (global location)

Which would you prefer?
```

### Step 4: Verify .gitignore

```bash
# Check if directory pattern in .gitignore
grep -q "^\.worktrees/$" .gitignore || grep -q "^worktrees/$" .gitignore
```

If not present, add it:
```bash
echo ".worktrees/" >> .gitignore
# or
echo "worktrees/" >> .gitignore
```

## Creating a Worktree

### Standard Creation

```bash
# Determine branch name from feature
BRANCH_NAME="feature/descriptive-name"

# Create worktree with new branch
git worktree add ".worktrees/$BRANCH_NAME" -b "$BRANCH_NAME"

# Navigate to worktree
cd ".worktrees/$BRANCH_NAME"
```

### From Existing Branch

```bash
git worktree add ".worktrees/$BRANCH_NAME" "$BRANCH_NAME"
```

## Post-Creation Setup

### Install Dependencies

```bash
# Node.js
if [ -f package.json ]; then npm 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 Initial Tests

```bash
# Run test suite
npm test  # or appropriate command

# Report status
```

If tests fail: Report failures, ask whether to proceed or investigate.
If tests pass: Report ready.

## Worktree Management

### List Worktrees

```bash
git worktree list
```

### Remove Worktree

```bash
# After merging feature branch
git worktree remove ".worktrees/feature/branch-name"

# Force remove (if needed)
git worktree remove --force ".worktrees/feature/branch-name"
```

### Prune Stale Worktrees

```bash
git worktree prune
```

## Best Practices

### Naming Convention
- `feature/descriptive-name` for features
- `bugfix/issue-number-description` for bugs
- `hotfix/critical-issue` for urgent fixes

### Keep Worktrees Focused
- One feature per worktree
- Merge and remove when done
- Don't let worktrees accumulate

### Sync Regularly
```bash
# In worktree, get latest from main
git fetch origin
git rebase origin/main
```

## Completion Checklist

When feature is complete:
1. [ ] All tests pass
2. [ ] Code reviewed
3. [ ] Merged to main
4. [ ] Worktree removed
5. [ ] Branch deleted (if desired)

```bash
# Full cleanup
git checkout main
git pull
git worktree remove ".worktrees/feature/name"
git branch -d feature/name
```

## Announcement Template

At start of worktree creation:
```
"I'm using the using-git-worktrees skill to set up an isolated workspace for [feature name]."
```

On completion:
```
"Worktree ready at [full-path]
Tests passing ([N] tests, 0 failures)
Ready to implement [feature-name]"
```

Overview

This skill creates isolated git worktrees for new feature or bugfix development so the main branch stays clean and uninterrupted. It automates safe directory selection, verifies .gitignore, bootstraps dependencies, runs initial tests, and provides cleanup commands. Use it to reduce context-switch friction and keep one clear purpose per workspace.

How this skill works

The skill checks for preferred worktree directories (.worktrees then worktrees) and honors any documented preference in a CLAUDE.md file. If no location exists, it prompts for project-local or global placement, ensures the chosen directory is ignored in .gitignore, then creates a git worktree for a new or existing branch. After creation it installs dependencies and runs tests, reporting status and offering next steps.

When to use it

  • Starting a new feature to keep changes isolated from main
  • Working on a bugfix while main branch continues active development
  • Switching contexts frequently without using stashes
  • Needing a clean, disposable workspace for experiments
  • Preparing a focused environment for CI-local testing before a PR

Best practices

  • Use a consistent naming convention (feature/, bugfix/, hotfix/) for branches
  • Create one worktree per feature and remove it after merging
  • Ensure the worktree directory is listed in .gitignore to avoid clutter
  • Run dependency install and the test suite immediately after creation
  • Prune stale worktrees regularly and delete branches once merged

Example use cases

  • Create .worktrees/feature/user-auth and run npm install and tests before opening a PR
  • Add a worktree from an existing branch to iterate on a hotfix without disturbing main
  • Switch to a different task by creating a second worktree instead of stashing local changes
  • Use a global worktrees location for multiple projects when disk layout demands it
  • Prune and remove worktrees after merge as part of a completion checklist

FAQ

What if both .worktrees and worktrees exist?

.worktrees takes precedence. The skill will use .worktrees if present to keep worktrees hidden by default.

Will the skill modify .gitignore automatically?

If the chosen worktree directory is not listed, the skill will add the appropriate entry (e.g., .worktrees/ or worktrees/) to .gitignore to prevent accidental commits.

How are dependencies handled after creating a worktree?

The skill detects common manifests (package.json, Cargo.toml, pyproject.toml, requirements.txt, go.mod) and runs the appropriate install or build commands. It then runs the test command you use in the project (e.g., npm test) and reports results.