Skip to content
Back to skills

Collection

BSecurity

Use git worktrees for parallel development on multiple branches simultaneously

  • 24 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 8, 2026
code-qualitygobashnodetestingcode-reviewgitdatabasesecurity

Works with

  • terminal

Security analysis

B89/100
  • highPerforms destructive filesystem operations
  • mediumInstalls packages at runtime which could introduce malicious dependencies

Pro scans all 21 files and shows the line behind each finding

Scanned September 8, 2026

npx -y skills add mattnigh/skills_collection --skill collection --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Collection?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Collection
[![Security: B — Skills Directory](https://www.skillsdirectory.com/api/skills/mattnigh-collection-afc22a58/badge)](https://www.skillsdirectory.com/skills/mattnigh-collection-afc22a58)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: git-worktrees
description: Use git worktrees for parallel development on multiple branches simultaneously
tags: [git, worktrees, parallel-development, productivity]
related_agents: [version-control, engineer]
---

# Git Worktrees

## Overview

Git worktrees allow you to have multiple working directories from the same repository, each with a different branch checked out. Work on multiple branches simultaneously without switching.

## When to Use Worktrees

### ✅ Perfect For:
- Stacked PR development (one worktree per PR)
- Urgent hotfix while working on feature
- Parallel development on multiple features
- Code review in isolation
- Testing cross-branch interactions
- Running multiple dev servers simultaneously

### ⚠️ Consider Alternatives When:
- Limited disk space (worktrees duplicate working directory)
- Simple branch switching is sufficient
- Only working on one branch at a time

## Basic Workflow

### Create Worktree

**New Branch:**
```bash
# Create worktree with new branch
git worktree add ../worktrees/feature-auth -b feature/authentication

# Navigate to worktree
cd ../worktrees/feature-auth
```

**Existing Branch:**
```bash
# Create worktree from existing remote branch
git worktree add ../worktrees/feature-profile feature/user-profile

# Or from origin
git worktree add ../worktrees/review origin/feature/pr-to-review
```

### List Worktrees

```bash
git worktree list

# Output:
# /Users/dev/project              abc123 [main]
# /Users/dev/worktrees/f-auth     def456 [feature/authentication]
# /Users/dev/worktrees/f-profile  ghi789 [feature/user-profile]
```

### Remove Worktree

```bash
# Remove worktree (deletes directory)
git worktree remove ../worktrees/feature-auth

# Or manually delete directory and prune
rm -rf ../worktrees/feature-auth
git worktree prune
```

## Directory Structure

Recommended layout:

```
/Users/dev/
├── my-project/              # Main repository
│   ├── .git/               # Git database
│   ├── src/
│   └── ...
└── my-project-worktrees/    # All worktrees here
    ├── feature-auth/       # feature/authentication branch
    ├── feature-profile/    # feature/user-profile branch
    ├── hotfix-urgent/      # hotfix/urgent-fix branch
    └── review-pr-123/      # Reviewing PR #123
```

## Use Case: Stacked PRs

Perfect for stacked PR workflow - one worktree per PR:

```bash
# Create worktree for each PR in stack
git worktree add ../stack/pr-001 -b feature/001-base-auth
git worktree add ../stack/pr-002 -b feature/002-user-profile
git worktree add ../stack/pr-003 -b feature/003-admin-panel

# Work in each independently
cd ../stack/pr-001
# Implement base auth
git commit -am "feat: base authentication"
git push -u origin feature/001-base-auth

cd ../stack/pr-002
# Already on feature/002-user-profile branch
# Implement user profile (depends on pr-001)
git commit -am "feat: user profile with auth"
git push -u origin feature/002-user-profile

cd ../stack/pr-003
# Implement admin panel (depends on pr-002)
git commit -am "feat: admin panel"
git push -u origin feature/003-admin-panel
```

## Use Case: Parallel Development

Run multiple dev servers simultaneously:

```bash
# Terminal 1: Main feature development
cd /project-worktrees/feature-new-ui
npm install
npm run dev  # Server on port 3000

# Terminal 2: Urgent hotfix (different branch)
cd /project-worktrees/hotfix-critical
npm install
npm run dev -- --port 3001  # Server on port 3001

# Both running simultaneously without branch switching
```

## Use Case: Code Review

Review PRs in isolation:

```bash
# Create worktree for PR review
git worktree add ../review/pr-456 origin/feature/user-auth

cd ../review/pr-456
npm install
npm test
npm run dev

# Review code, test functionality
# When done, remove worktree
cd /main-project
git worktree remove ../review/pr-456
```

## Updating Stacked PRs with Worktrees

When base PR changes, update chain across worktrees:

```bash
# PR-001 got feedback
cd /stack/pr-001
git pull origin feature/001-base-auth
# Make changes, push

# Update PR-002 (in separate worktree)
cd /stack/pr-002
git rebase feature/001-base-auth
git push --force-with-lease origin feature/002-user-profile

# Update PR-003 (in separate worktree)
cd /stack/pr-003
git rebase feature/002-user-profile
git push --force-with-lease origin feature/003-admin-panel
```

## Managing Dependencies

### Shared node_modules (Save Disk Space)

**Option 1: Symlink**
```bash
cd /worktrees/feature-auth
ln -s /main-project/node_modules node_modules
```

**Option 2: Separate Install**
```bash
cd /worktrees/feature-auth
npm install  # Independent node_modules
```

Trade-off:
- Symlink: Less disk space, may have version conflicts
- Separate: More disk space, guaranteed isolation

## Best Practices

### 1. Naming Convention
```bash
# Use descriptive, consistent names
git worktree add ../worktrees/feature-authentication feature/authentication
git worktree add ../worktrees/hotfix-security hotfix/security-patch
```

### 2. Location Strategy
```bash
# Keep worktrees outside main repo
/Users/dev/project/              # Main repo (never delete)
/Users/dev/project-worktrees/    # All worktrees here (safe to delete)
```

### 3. Cleanup Discipline
```bash
# When PR merged, remove worktree immediately
git worktree remove path/to/worktree

# Periodically check for stale worktrees
git worktree prune

# Delete merged branches
git branch -d feature/old-branch
git push origin --delete feature/old-branch
```

### 4. One Branch Per Worktree
```
❌ WRONG: Switching branches in worktree defeats the purpose
✅ CORRECT: Each worktree permanently on one branch
```

## Common Commands

```bash
# Create worktree with new branch
git worktree add <path> -b <branch>

# Create worktree from existing branch
git worktree add <path> <branch>

# List all worktrees
git worktree list

# Remove worktree
git worktree remove <path>

# Clean up stale references
git worktree prune

# Move worktree to different location
git worktree move <old-path> <new-path>
```

## Troubleshooting

### Issue: "fatal: '<branch>' is already checked out"

**Cause:** Branch is checked out in another worktree

**Solution:**
```bash
# List worktrees to find where branch is checked out
git worktree list

# Either work in existing worktree or remove it first
git worktree remove <path-to-old-worktree>
```

### Issue: Disk space concerns

**Solution:**
- Use symlinks for node_modules
- Remove worktrees when PRs merged
- Run `git worktree prune` regularly
- Consider using sparse-checkout for large repos

### Issue: IDE confusion with multiple worktrees

**Solution:**
- Open each worktree as separate workspace
- Use IDE's multi-window/split-workspace features
- Name worktrees descriptively for easy identification

## Agent Instructions

When delegating worktree setup to version-control agent:

```
Task: Create worktrees for stacked PR development

Requirements:
- Create 3 worktrees in /project-worktrees/
- Worktree 1: pr-001 with branch feature/001-base-auth
- Worktree 2: pr-002 with branch feature/002-user-profile
- Worktree 3: pr-003 with branch feature/003-admin-panel

Commands:
git worktree add ../project-worktrees/pr-001 -b feature/001-base-auth
git worktree add ../project-worktrees/pr-002 -b feature/002-user-profile
git worktree add ../project-worktrees/pr-003 -b feature/003-admin-panel

Verification: git worktree list should show all 3 worktrees
```

## Benefits

✅ **No Branch Switching:** Work on multiple branches without `git checkout`
✅ **Parallel Servers:** Run multiple dev environments simultaneously
✅ **Preserve State:** Build artifacts and node_modules stay per-branch
✅ **Safer Reviews:** Test PRs without affecting main working directory
✅ **Faster Context Switch:** Jump between worktrees instead of rebasing

## Related Skills

- `stacked-prs` - Combine worktrees with stacked PR workflow
- `git-workflow` - General git branching patterns
- `code-review` - Review code in isolated worktrees

Files in this skill

  • 0Chan-smc__claude-code-workflow-lab__claude__skills__frontend-dev-guidelines__SKILL.md15.1 KB
  • 17hz__nextjs-template__claude__skills__example-skill__SKILL.md316 B
  • 1ambda__dataops-platform__claude__skills__context-synthesis__SKILL.md3.5 KB
  • 1natsu172__dotfiles__claude__skills__git-analysis__SKILL.md5.4 KB
  • 1natsu172__dotfiles__claude__skills__github-pr-best-practices__SKILL.md7.7 KB
  • 23Maestro__prospect-pipeline__claude__skills__npid-fastapi-skill.md26.1 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-code-javascript__SKILL.md15.7 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-code-python__SKILL.md17.5 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-expression-syntax__SKILL.md9.4 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-mcp-tools-expert__SKILL.md12.5 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-node-configuration__SKILL.md16.6 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-workflow-patterns__SKILL.md11.2 KB
  • 3x-Projetos__claude-memory-framework__claude__skills__scientist__SKILL.md14.8 KB
  • 5MinFutures__futures-arena__claude__skills__migration-tracker__SKILL.md16.2 KB
  • 5MinFutures__futures-arena__claude__skills__planning-guidelines__SKILL.md11.8 KB
  • 92Bilal26__TaskPilotAI__claude__skills__assessment-builder__SKILL.md17.5 KB
  • 92Bilal26__TaskPilotAI__claude__skills__book-scaffolding__SKILL.md19.1 KB
  • 92Bilal26__TaskPilotAI__claude__skills__code-validation-sandbox__SKILL.md6.2 KB
  • 92Bilal26__TaskPilotAI__claude__skills__exercise-designer__SKILL.md18.1 KB
  • 92Bilal26__TaskPilotAI__claude__skills__learning-objectives__SKILL.md24.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in code-quality →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.