Skip to content
Back to skills

Using Git Worktrees

ASecurity

Use before starting infrastructure or feature work that needs isolation from the current workspace — ensures an isolated workspace exists via native tools or git worktree fallback

  • 2 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added November 14, 2026
devopsgobashterraformgitdevops

Works with

  • cli

Security analysis

A100/100

Scanned November 14, 2026

npx -y skills add nikospavlopoulos/devops-mutant-powers --skill using-git-worktrees --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Using Git Worktrees?

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

Security grade badge for Using Git Worktrees
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/nikospavlopoulos-using-git-worktrees/badge)](https://www.skillsdirectory.com/skills/nikospavlopoulos-using-git-worktrees)

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: using-git-worktrees
description: Use before starting infrastructure or feature work that needs isolation from the current workspace — ensures an isolated workspace exists via native tools or git worktree fallback
---

# Using Git Worktrees

## Overview

Ensure work happens in an isolated workspace. Prefer your platform's native worktree tools. Fall back to manual git worktrees only when no native tool is available.

**Core principle:** Detect existing isolation first. Then use native tools. Then fall back to git. Never fight the harness.

**Announce at start:** "I'm using the using-git-worktrees skill to set up an isolated workspace."

## Step 0: Detect Existing Isolation

**Before creating anything, check if you are already in an isolated workspace.**

```bash
GIT_DIR=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P)
GIT_COMMON=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P)
BRANCH=$(git branch --show-current)
```

**Submodule guard:** `GIT_DIR != GIT_COMMON` is also true inside git submodules. Before concluding "already in a worktree," verify:

```bash
# If this returns a path, you're in a submodule — treat as normal repo
git rev-parse --show-superproject-working-tree 2>/dev/null
```

**If `GIT_DIR != GIT_COMMON` (and not a submodule):** Already in a linked worktree. Skip to Step 3.

Report:
- On a branch: "Already in isolated workspace at `<path>` on branch `<name>`."
- Detached HEAD: "Already in isolated workspace at `<path>` (detached HEAD, externally managed)."

**If `GIT_DIR == GIT_COMMON` (or in a submodule):** Normal repo checkout. Ask for consent:

> "Would you like me to set up an isolated worktree? It protects your current branch from changes."

If user declines, work in place and skip to Step 3.

## Step 1: Create Isolated Workspace

**Try in this order:**

### 1a. Native Worktree Tools (preferred)

Do you have a tool like `EnterWorktree`, `WorktreeCreate`, `/worktree`, or `--worktree`? If so, use it and skip to Step 3.

Native tools handle directory placement, branch creation, and cleanup automatically.

### 1b. Git Worktree Fallback

Only when no native tool is available:

```bash
# Create worktree with a new branch
git worktree add -b feature/<branch-name> .worktrees/<branch-name> main

# Or for a specific base branch
git worktree add -b infra/<change-name> .worktrees/<change-name> origin/main

# List worktrees
git worktree list
```

## Step 2: Enter the Worktree

```bash
cd .worktrees/<branch-name>
# All changes here are isolated from the main working directory
```

## Step 3: Project Setup

Once in the isolated workspace, confirm you're on the expected branch and proceed with the planned work.

## Cleanup

Cleanup is handled by `devops-mutant-powers:finishing-a-development-branch` at the end of the work. Only clean up worktrees you created:

```bash
# From main repo root (NOT from inside the worktree)
MAIN_ROOT=$(git -C "$(git rev-parse --git-common-dir)/.." rev-parse --show-toplevel)
cd "$MAIN_ROOT"
git worktree remove .worktrees/<branch-name>
git worktree prune  # clean up stale registrations
```
Worktree ready at <full-path>
Tests passing (<N> tests, 0 failures)
Ready to implement <feature-name>
```

## Quick Reference

| Situation | Action |
|-----------|--------|
| Already in linked worktree | Skip creation (Step 0) |
| In a submodule | Treat as normal repo (Step 0 guard) |
| Native worktree tool available | Use it (Step 1a) |
| No native tool | Git worktree fallback (Step 1b) |
| `.worktrees/` exists | Use it (verify ignored) |
| `worktrees/` exists | Use it (verify ignored) |
| Both exist | Use `.worktrees/` |
| Neither exists | Check instruction file, then default `.worktrees/` |
| Directory not ignored | Add to .gitignore + commit |
| Permission error on create | Sandbox fallback, work in place |
| Tests fail during baseline | Report failures + ask |
| No package.json/Cargo.toml | Skip dependency install |

## Common Rationalizations

| Excuse | Reality |
|--------|---------|
| "I'm obviously not in a worktree — no need to check" | Run Step 0. Harness-created isolation and submodules both fool eyeballing; the detection commands settle it. |
| "`git worktree add` is quicker than hunting for a native tool" | A native tool (e.g. `EnterWorktree`) owns placement, branching, and cleanup. Bypassing it is the #1 mistake — it creates phantom state your harness can't see or manage. |
| "The worktree directory is surely ignored already" | Run `git check-ignore`. An unignored worktree directory commits the whole tree into the repo. |
| "Any directory name works" | Explicit instructions beat an existing project-local directory, which beats the `.worktrees/` default. |
| "The workspace is fresh — baseline tests can wait" | A dirty baseline makes every later failure ambiguous. Run the tests now; proceeding past failures is your human partner's call. |

## Safety Rules

- Never run destructive infra commands (terraform destroy, kubectl delete) without confirming the target environment
- Always confirm which branch/worktree you're in before applying changes
- Do NOT run `git worktree remove` from inside the worktree being removed
- Only clean up worktrees under `.worktrees/` or `worktrees/`
- Run `git worktree prune` after removal as self-healing

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 devops →

Comments

Loading comments…

Newsletter

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