agentleFS
darinhdarinh/copilot-tools/skills/worktrees/SKILL.md

worktrees

Skill1 starsChanged 4 months agoMITView on GitHub

Creating, finishing, recovering, and safely delegating git worktrees. Load this skill before creating a worktree, merging one back, cleaning one up, or handing one to a subagent — not at session start.

20 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use resolve-conflicts, debug and 9 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.
  • ralphkraussSkill

    resolve-conflicts

    Resolve git merge conflicts or re-apply changes from a stale branch onto current main. Use when user says "resolve conflicts", "merge conflicts", "rebase branch", "branch is behind", "re-apply changes", "port branch", or a branch is too far behind main to merge cleanly.

    Also in 3 projects that use this

  • AnthropicSkill

    debug

    Structured debugging session — reproduce, isolate, diagnose, and fix. Trigger with an error message or stack trace, "this works in staging but not prod", "something broke after the deploy", or when behavior diverges from expected and the cause isn't obvious.

    Also in 4 projects that use this

  • GoogleSkill

    prototype

    Build a throwaway prototype to answer a design question. Use when the user wants to sanity-check whether a state model or logic feels right, or explore what a UI should look like.

    Also in 4 projects that use this

  • GoogleSkill

    improve-codebase-architecture

    Scan a codebase for deepening opportunities, present them as a visual HTML report, then grill through whichever one you pick.

    Also in 4 projects that use this

  • MicrosoftSkill

    handoff

    Compact the current conversation into a handoff document for another agent to pick up.

    Also in 6 projects that use this

  • BlockSkill

    plan

    Draft a TDD, PRD, or lightweight plan in the conversation without committing it.

    Also in 3 projects that use this

  • +1 more from google

The file

We split this skill into the rules it gives an agent

8 rules. 0 are common, 5 are distinctive, and 3 are very specific to this project.

  • 0

    Common

    Most of the 20 edited versions we read keep these.

  • 5

    Distinctive

    This author's own choices. Worth a look.

  • 3

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use that for anything identifying the project — project directory, .specify/ init, config lookups.

    line 26
  2. Distinctive:

    Prefer the operator commands.

    line 45
  3. Distinctive:

    Never create a worktree inside another worktree.

    line 80
  4. Distinctive:

    Leave worktrees you did not create alone.

    line 82
  5. Distinctive:

    Every probe script, every reproduction written to confirm a bug, every scratch copy kept for a diff.

    line 87
  6. Distinctive:

    Verify the tree before you act on the findings: If a subagent mentions in passing that something of yours was lost, stop and check git …

    line 118
  7. Very specific:

    Only after operator confirms the owner is gone — operator worktree recover reports, it does not decide for you.

    line 134
  8. Very specific:

    If there are uncommitted changes, commit them to a wip/ branch first (that is what --preserve does), then continue.

    line 136

“Common” is measured: kept by at least 6 in 10 of the 20 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

Worktrees

Operator assigns your worktree before your session starts. You need this skill only when you are changing the set of worktrees, not to work in the one you were given.

Resolving the primary root

Inside a worktree, git rev-parse --show-toplevel returns the worktree. The first record of git worktree list --porcelain is always the primary checkout, from anywhere in the repo:

repo_root=$(git worktree list --porcelain | head -1 | cut -d' ' -f2-)
$repoRoot = (git worktree list --porcelain | Select-Object -First 1) -replace '^worktree '

Use that for anything identifying the project — project directory, .specify/ init, config lookups. Using the worktree path instead mints a duplicate project id and splits the project's state in two, silently.

It is also why "walk up until you find AGENTS.md" is not a way to find the repo root: in a monorepo it finds a subproject's file, and inside a worktree it finds the worktree's own tracked copy. Neither is the primary checkout.

Layout

<repoRoot>/.worktrees/<name>, where <name> is the branch with / replaced by - (feat/login → .worktrees/feat-login). /.worktrees/ must be in the tracked .gitignore — .git/info/exclude does not reach other clones. operator worktree new adds the rule if it is absent, reports that it did, and leaves it unstaged for you to commit deliberately. It recognises a rule you wrote yourself in any spelling git accepts and leaves it alone.

Lifecycle

Prefer the operator commands. They record the worktree against your work item, so liveness recovery can find it later; a plain git worktree add leaves a tree nobody can attribute to a claim.

operator worktree new     --instance NAME --item REF [--project SUB] [--branch NAME] [--path PATH]
operator worktree finish  --instance NAME [--item REF] [--into REF]
operator worktree recover [--preserve]

A checkout is 1:1 with a work item: new takes the claim and creates the tree together, and releases the claim again if the tree cannot be made — so a failed create never leaves an item claimed by nobody.

finish refuses a tree with uncommitted changes rather than tidying it, and deletes the branch only when --into already contains it. Both refusals are the point: the alternative to refusing is discarding work whose value only its author knows.

recover reports and removes nothing. --preserve commits the uncommitted work of an unclaimed tree, or one whose owner is provably gone, to a wip/ branch.

Underlying git, if you need it:

git -C "$repo_root" worktree add .worktrees/feat-login -b feat/login
cd "$repo_root/.worktrees/feat-login"
# work, commit
cd "$repo_root" && git merge --no-ff feat/login
git worktree remove .worktrees/feat-login && git branch -d feat/login

Rules

  • One worktree per branch — git refuses two.
  • Never create a worktree inside another worktree. Resolve the primary root first.
  • cd out before removing.
  • Leave worktrees you did not create alone. Another agent may be live in one.
  • Worktree branches merge to main. There is no integration branch.

Scratch files never go in a checkout

Every probe script, every reproduction written to confirm a bug, every scratch copy kept for a diff. A script with a relative path writes wherever the process happens to be, and for an agent that is almost always somebody's checkout.

scratch=$(mktemp -d)
$scratch = New-Item -ItemType Directory -Path (Join-Path $env:TEMP ([guid]::NewGuid()))

Tell your subagents the same thing, by name. They run their own shell in your tree and you see only their output.

git status will not save you — git does not track empty directories, so an empty stray is invisible to it. A checkout can report perfectly clean with artifacts sitting in its root. Clean up before you finish, not later: an artifact found afterwards has no provenance, and every explanation fits it equally well. Three agents once spent an evening diagnosing a working-directory bug that did not exist, on the evidence of directories their own review subagents had left behind.

Handing a worktree to a subagent

Commit before you delegate. Staging is not enough. A commit is the only state a subagent cannot casually destroy. Point reviewers at git diff main...HEAD, not git diff --staged.

Forbid mutating git commands by name: stash, checkout, reset, clean, restore, rebase, commit, add. "Do not write outside tmp" does not cover git plumbing, which creates no new files.

Verify the tree before you act on the findings. If a subagent mentions in passing that something of yours was lost, stop and check git status and git stash list before reading anything else it reported. Work that was git add-ed survives a dropped stash as dangling objects:

git fsck --unreachable
git cat-file -p <blob>     # grep for a string unique to your change

A subagent that ran git checkout or reset --hard instead leaves nothing to recover at all. A reviewer once destroyed 454 lines this way and mentioned it in passing in an otherwise clean review; see docs/rationale.md.

Recovering someone else's worktree

Only after operator confirms the owner is gone — operator worktree recover reports, it does not decide for you. Never stash, reset, clean, or delete. If there are uncommitted changes, commit them to a wip/ branch first (that is what --preserve does), then continue. A crashed agent's uncommitted work is the most expensive thing in the tree.

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.