agentleFS
flyer103flyer103/skills/git-worktree/SKILL.md

git-worktree

Skill1 starsChanged 5 months agoMITView on GitHub

Git worktree management for AI Agents — create, use, and clean up isolated working trees. Use when an agent needs to work on multiple branches simultaneously without cloning.

  • Deletes or force-pushes

30 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use agent-native-architecture, document-review and 7 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • CodeWithBehnamSkill

    agent-native-architecture

    Build applications where agents are first-class citizens. Use this skill when designing autonomous agents, creating MCP tools, implementing self-modifying systems, or building apps where features are outcomes achieved by agents operating in a loop.

    Also in 3 projects that use this

  • CodeWithBehnamSkill

    document-review

    This skill should be used to refine brainstorm or plan documents before proceeding to the next workflow step. It applies when a brainstorm or plan document exists and the user wants to improve it.

    Also in 3 projects that use this

  • MicrosoftSkill

    git-commit

    Stage user-selected paths, confirm the exact staged set, and create one local Conventional Commit, or generate a commit message for staged changes without committing. Use when you want a guarded commit or a ready-to-paste message.

    Also in 3 projects that use this

  • MicrosoftSkill

    grill-with-docs

    Grilling session that challenges your plan against the existing domain model, sharpens terminology, and updates documentation (CONTEXT.md, ADRs) inline as decisions crystallise. Use when user wants to stress-test a plan against their project's language and documented decisions.

    Also in 3 projects that use this

  • OpenAISkill

    brainstorming

    You MUST use this before any creative work - creating features, building components, adding functionality, or modifying behavior. Explores user intent, requirements and design before implementation.

    Also in 3 projects that use this

  • BlockSkill

    frontend-design

    Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

    Also in 4 projects that use this

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 2 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 25 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Solution: Always use git worktree remove.

    line 139
  2. Very specific:

    Solution: Test the no-remote scenario separately.

    line 143
  3. Distinctive:

    Solution: Use absolute paths or anchor via git worktree list --porcelain to obtain the path.

    line 147

“Common” is measured: kept by at least 6 in 10 of the 25 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Git Worktree — Agent Workspace Isolation

Agents use git worktree to safely work in parallel across multiple branches. Covers the full lifecycle of creation, switching, and cleanup, as well as concurrency safety pitfalls.

Triggers

  • Agent needs to work on multiple branches simultaneously in the same repository
  • Agent needs an isolated workspace to avoid polluting the main working directory
  • Kanban / pipeline requires per-task independent checkouts
  • User mentions "git worktree", "parallel development", "multi-branch simultaneous work"

Requirements

  • Git >= 2.35 (recommended) — 2.5+ usable, 2.35+ includes fetch protection for all worktree branches
  • Verify: git --version

Lifecycle

1. Create Worktree

# Standard creation (auto-creates branch with same name)
git worktree add <path> -b <branch-name>

# Agent recommended pattern: internal directory + task ID naming
WORKTREE_DIR=".worktrees/$(date +%s)-${TASK_ID}"
git worktree add "$WORKTREE_DIR" -b "wt/${TASK_ID}"

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

# Detached HEAD (temporary work)
git worktree add -d <path>

2. Use Worktree

cd <worktree-path>
# Normal development...
git status
git add ...
git commit ...

3. List & Status

git worktree list                    # Human-readable
git worktree list --porcelain        # Script parsing
git worktree list --verbose          # Includes prune information

4. Lock Protection

# Lock an active worktree (prevents automatic pruning)
git worktree lock <path> --reason "active agent task TASK-123"

# Unlock
git worktree unlock <path>

5. Cleanup

# Standard cleanup (requires clean working tree + no unpushed commits)
git worktree remove <path>

# Force cleanup
git worktree remove --force <path>

# Clean up metadata (must run after manual rm -rf of directory)
git worktree prune

# Clean by expiration age
git worktree prune --expire 7.days.ago

Recommended Patterns for Agents

Per-Task Worktree (Kanban / Pipeline)

# Create
TASK_ID="t_8894c1db"
WORKTREE=".worktrees/${TASK_ID}"
git worktree add "$WORKTREE" -b "wt/${TASK_ID}"

# Clean up after task completion
git worktree remove "$WORKTREE" 2>/dev/null || {
  # Dirty worktree protection: lock + preserve
  git worktree lock "$WORKTREE" --reason "dirty: ${TASK_ID}"
  echo "WARNING: worktree $WORKTREE preserved (dirty)"
}
git worktree prune

Multi-Agent Parallel Pattern

# Agent 1
git worktree add .worktrees/agent-1 -b wt/agent-1

# Agent 2
git worktree add .worktrees/agent-2 -b wt/agent-2

# Each agent works independently in its own worktree
# Note: branch names must be unique (guaranteed by naming convention)

Concurrency Safety Rules

RuleDescription
Unique branch namesEach worktree must be bound to a different branch (or detached HEAD)
Protect with lockLock active worktrees; git worktree prune skips locked entries
Prefer remove over rm -rfgit worktree remove cleans metadata; after manual rm -rf, run git worktree prune
Internal directoryUse .worktrees/ instead of external paths for unified management

Pitfalls

1. Branch Name Conflict

fatal: 'wt/task-123' is already checked out by '.worktrees/task-123'

Cause: Multiple agents attempt to create a worktree with the same name from the same working directory. Solution: Include a unique ID (timestamp / task ID) in the branch name.

2. Manual rm -rf Leaves Stale Metadata

Symptom: git worktree list still shows the deleted worktree (marked as prunable). Solution: Always use git worktree remove; if already manually deleted, run git worktree prune.

3. No Remote Repository Scenario

Symptom: git worktree remove behaves differently in local repositories (never pushed). Solution: Test the no-remote scenario separately; --force may be required.

4. Relative Path Resolution

Symptom: Agent frameworks may resolve file operations to the process cwd rather than the worktree path. Solution: Use absolute paths or anchor via git worktree list --porcelain to obtain the path.

5. Stale Worktree Accumulation

Symptom: Abandoned worktrees consume disk space (100 MB–2 GB+ each). Solution: Implement a cleanup policy — skip if < 24h, delete if 24h–72h with no push, force delete if 72h+.

Configuration Recommendations

# Auto-cleanup configuration
git config gc.worktreePruneExpire 7.days.ago

# Agent environments can be more aggressive
git config gc.worktreePruneExpire 3.days.ago

Quick Reference

OperationCommand
Creategit worktree add <path> -b <branch>
Listgit worktree list
Lockgit worktree lock <path> --reason "..."
Unlockgit worktree unlock <path>
Removegit worktree remove <path>
Prunegit worktree prune
Movegit worktree move <old> <new>
Repairgit worktree repair [<path>...]

Sources

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.