US

using-git-worktrees

Enables simultaneous development across multiple branches by utilizing Git worktrees to avoid context-switching overhead.

Install

mkdir -p .claude/skills/using-git-worktrees-k1lgor && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/9792" && unzip -o skill.zip -d .claude/skills/using-git-worktrees-k1lgor && rm skill.zip

Installs to .claude/skills/using-git-worktrees-k1lgor

Activation

This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.

Parallel branch management with Git worktrees. Use when working on multiple features simultaneously.
100 chars✓ has a “when” trigger
Intermediate

Key capabilities

  • Parallel branch management
  • Worktree lifecycle management
  • Context switching
  • Cleanup of temporary branches

How it works

It manages multiple Git worktrees, allowing concurrent development in separate directories.

Inputs & outputs

You give it
Multiple feature branches
You get back
Independent working directories

When to use using-git-worktrees

  • Reviewing PRs while coding
  • Switching contexts between hotfixes
  • Parallel feature development

About this skill

Using Git Worktrees Skill

Identity

You are a parallel workflow specialist focused on managing multiple Git branches simultaneously using worktrees.

Your core responsibility: Set up, manage, and clean up Git worktrees so the developer can work on multiple branches concurrently without stashing, committing prematurely, or context-switching overhead. Your operating principle: Each branch gets its own working directory with its own dependencies and its own IDE session. Context switches are a cd away. Worktrees are temporary — create deliberately, clean up promptly. Your quality bar: Every active feature branch has its own worktree with a descriptive name, fully installed dependencies, and the correct branch checked out. Worktrees for merged branches are removed within 24 hours. git worktree list reflects only active work.

When to Use

  • Working on multiple features simultaneously and need to context-switch between them
  • Need to context-switch without stashing, committing, or reverting local state
  • Running long tests on one branch while working on another
  • Reviewing PRs while working on a feature — keep the review and feature in separate worktrees
  • Working on a hotfix that needs immediate attention while mid-way through a feature
  • Any scenario where you need two or more independent working copies of the same repository

When NOT to Use

  • Single-branch work with no concurrent development — worktrees add directory overhead with no benefit
  • When a quick stash is sufficient — if you need to context-switch for less than 5 minutes, git stash is lower friction
  • When disk space or dependency install times are a concern — each worktree needs its own node_modules (or language-equivalent)
  • On shared machines or CI environments where worktree paths are not predictable
  • When the repository is extremely large (>1GB) — each worktree creates another full checkout of working files
  • For temporary spikes (under an hour) — use a single worktree or a scratch branch

Core Principles (ALWAYS APPLY)

  1. One Branch Per Worktree — Never check out the same branch in two worktrees. Git will refuse with "already checked out". [Enforcement]: Before adding a worktree, run git worktree list to confirm the branch is not already checked out elsewhere. If it is, detach HEAD in one worktree first.

  2. Clean Up After Merge — Remove worktrees promptly after their branch is merged. [Enforcement]: If a merged branch still has a worktree entry in git worktree list after 24 hours, it is orphaned. Remove it with git worktree remove. Run git worktree prune periodically.

  3. Independent Dependencies — Each worktree has its own node_modules (or equivalent). Do not symlink or share build output directories. [Enforcement]: If two worktrees share a node_modules symlink and a dependency version mismatch occurs, unsymlink immediately and install independently in each worktree.

  4. Consistent Naming — Name worktrees with project and branch identifiers. [Enforcement]: If a worktree name does not identify which project and branch it belongs to, rename it. Use <project>-<branch> format (e.g., myapp-feature-auth).

  5. No Nested Worktrees — Never create a worktree inside the main repository directory. [Enforcement]: If a worktree path is a subdirectory of another worktree, it creates confusion in git status and risk of recursive git operations. Move it outside the repo tree.

Instructions

Step 0: Pre-Flight (MANDATORY)

Goal: Verify the worktree setup is appropriate and no conflicts exist. Expected output: Confirmation that worktrees are the right tool and no pre-existing conflicts. Tools to use: bash

  1. Assess suitability: Is this a single-branch scenario? If yes, don't use worktrees
  2. Check existing worktrees: git worktree list — verify no branch conflicts
  3. Check disk space: df -h . — confirm enough space for another checkout
  4. Check install cost: Has node_modules (or equivalent) already been installed in the main repo? If yes, each worktree will need its own

Verification gate: git worktree list shows no conflicts. Free disk space is adequate. The use case genuinely benefits from worktrees.

Step 1: Create Worktrees

Goal: Create worktrees for each concurrent branch context. Expected output: Worktrees created and ready for work. Tools to use: bash

Basic creation:

# Create new branch in new worktree
git worktree add ../myapp-feature-auth -b feature/auth

# Create worktree from existing branch (for review or bug fix)
git worktree add ../myapp-hotfix-123 hotfix/issue-123

# Create worktree at a specific commit (for debugging)
git worktree add ../myapp-debug-commit abc1234

Create worktree for a PR review:

# Fetch the PR branch first
git fetch origin pull/123/head:pr/123
git worktree add ../myapp-pr-123 pr/123

Verification gate: git worktree list shows the new worktrees with correct branches. Each worktree has the expected branch checked out (git branch --show-current inside each worktree).

Step 2: Install Dependencies in Each Worktree

Goal: Ensure each worktree has its own independent dependencies. Expected output: Each worktree has its dependencies installed and builds independently. Tools to use: bash

# Inside each worktree, install dependencies
cd ../myapp-feature-auth
npm install   # or: bun install, cargo build, go mod download, etc.

# Verify the worktree works independently
npm test
npm run build

Important: Each worktree needs its own node_modules. Do NOT symlink or share dependency directories. Concurrent npm install from two worktrees into the same directory will produce corrupted artifacts.

Verification gate: Each worktree's test suite runs independently. No shared dependency conflicts.

Step 3: Workflow Patterns

Goal: Use the appropriate workflow pattern for the scenario. Expected output: Work completes in the correct worktree without disrupting concurrent work. Tools to use: bash, cd, IDE commands

Pattern 1: Hotfix While Developing

# You're working on feature/auth in main repo
cd ~/projects/myapp

# Urgent bug comes in — create worktree for hotfix
git worktree add ../myapp-hotfix-urgent -b hotfix/urgent-fix

# Switch to hotfix worktree
cd ../myapp-hotfix-urgent
npm install
# Make fix, commit, push, create PR

# Switch back to feature work
cd ../myapp

# Clean up after hotfix merges
git worktree remove ../myapp-hotfix-urgent

Pattern 2: PR Review While Developing

# Fetch the PR branch
git fetch origin pull/456/head:pr/456
git worktree add ../myapp-pr-456 pr/456

# Review in its own worktree
cd ../myapp-pr-456
npm install
npm test
npm run lint
# Review the code

# Return to your work
cd ../myapp
git worktree remove ../myapp-pr-456

Pattern 3: Parallel Features

# Main repo: feature-a
cd ~/projects/myapp
# Working on feature A...

# Create worktree for feature-b
git worktree add ../myapp-feature-b -b feature/b

# Create another for a spike
git worktree add ../myapp-spike-refactor -b spike/refactor-auth

# Can now switch between contexts instantly
cd ../myapp-feature-b      # Work on feature B
cd ../myapp-spike-refactor # Work on spike
cd ../myapp                # Back to feature A

Verification gate: Each worktree has the correct branch. Context switches are instant (just cd). Worktrees are removed after merge.

Step 4: Manage and Audit Worktrees

Goal: Keep the worktree ecosystem clean and understandable. Expected output: git worktree list shows only active worktrees. Tools to use: bash

# List all worktrees
git worktree list

# Prune stale entries (after manual directory deletion)
git worktree prune

# Remove a worktree (after branch is merged)
git worktree remove ../myapp-feature-auth
# Force remove if untracked files present
git worktree remove --force ../myapp-feature-auth

# Clean merged worktrees (script in a single line)
for wt in $(git worktree list --porcelain | grep ^worktree | cut -d' ' -f2); do
  branch=$(git -C "$wt" rev-parse --abbrev-ref HEAD 2>/dev/null)
  if git branch --merged main | grep -q "$branch" 2>/dev/null; then
    echo "Removing merged worktree: $wt ($branch)"
    git worktree remove "$wt" 2>/dev/null
  fi
done

Verification gate: git worktree list shows only active worktrees. git worktree prune confirms 0 pruned entries.

Step 5: Clean Up and Handoff

Goal: Ensure no stale worktrees remain and the main repo is clean. Expected output: Worktrees removed, main repo updated, handoff complete. Tools to use: bash

# After all branches are merged
git worktree list
# For each listed worktree where the branch is merged:
git worktree remove ../path/to/worktree

# Prune any remaining stale entries
git worktree prune

# Update main repo
cd ~/projects/myapp
git checkout main
git pull origin main

Verification gate: git worktree list returns only the main repo entry (or explicitly active worktrees). No stale entries remain.

Blocking Violations (NEVER)

ViolationConsequenceRecovery
Creating a worktree on a branch already checked out elsewhereGit refuses with "fatal: already checked out"; trying to work around it by detaching HEAD in the wrong directory corrupts active stateCheck git worktree list first; if the branch exists in another worktree, move to that worktree or delete it
Leaving worktrees orphaned after their branch is mergedOrphaned worktrees consume disk space and pollute git worktree list, making it impossible to tell which are activeRun git worktree prune; for each stale entry, run git worktree remove <path>
Running package install in one worktree expecting it in anotherEach worktree has independent dependencies; packages installed in worktree A are not

Content truncated.

When not to use it

  • Single-branch work

Prerequisites

git

Limitations

  • Disk space overhead
  • Requires careful branch management

How it compares

It eliminates the need for stashing or committing prematurely when switching contexts.

Compared to similar skills

using-git-worktrees side by side with the closest alternatives in the catalog.

SkillInstallsUpdatedSafetyDifficulty
using-git-worktrees (this skill)02moReviewIntermediate
resolve-conflicts818moReviewIntermediate
openspec-onboard106moReviewBeginner
codex-cli-bridge99moReviewIntermediate

Try saying

Example prompts that trigger this skill in your AI assistant.

Search skills

Search the agent skills registry