worktree

worktree is a skill for Claude Code, Codex from DJRHails/dotfiles. It costs 109 tokens per session (1,718 once invoked), scanned A, original, MIT.

A workflow for managing Git worktrees, which are extra working folders linked to branches in one repository. It sets them up, removes stale ones, and recovers broken worktrees while checking related pull requests.

In plain words
What is it for?
It helps create worktrees for open pull requests, clean up worktrees after merges, sync them with branches, and repair dangling worktree records.
Why use it?
It prevents old worktrees from accumulating and helps avoid losing time when a worktree no longer behaves like a Git repository.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one. Also seen: positional $N argument.

Needs its repository: it reads a path above its own folder, which exists only inside the repository. The line is echo "../.." > ".git/worktrees/$name/commondir".

Good fit It helps create worktrees for open pull requests, clean up worktrees after merges, sync them with branches, and repair dangling worktree records.

Compare 6 skills from other repositories ↓
Install

Getting it into your agent

It runs from inside its repository, so the clone comes first — what it calls does not travel with the file alone.

Clone the repo
git clone --depth 1 https://github.com/DJRHails/dotfiles
agentmods
npx agentmods add skills/djrhails/dotfiles/worktree

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for worktree

README.md
[![agentmods](https://agentmods.dev/badge/skills/djrhails/dotfiles/worktree.svg)](https://agentmods.dev/skills/djrhails/dotfiles/worktree)
Your own site
<a href="https://agentmods.dev/skills/djrhails/dotfiles/worktree"><img src="https://agentmods.dev/badge/skills/djrhails/dotfiles/worktree.svg" alt="Measured on agentmods" height="20"></a>
Per session 109 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,718 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00109 $0.01718
Opus 5 $0.00055 $0.00859
Sonnet 5 $0.00022 $0.00344
Haiku 4.5 $0.00011 $0.00172

Measured 4d ago against content hash 354d487289ed, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-07, from the pricing page.

Security

Grade A, and why

worktree scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 4d ago.

The scan reads SKILL.md. This mod also ships 1 executable file (scripts/worktree-create.sh), listed below but not scanned — reading those needs a real analyzer, not pattern matching.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

modules/agents/skills/worktree/SKILL.md · 193 lines

How it starts

The opening of the file, as written. The whole thing — 193 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Worktree Management

A single workflow that keeps worktrees in sync with open PRs: removes stale ones, creates missing ones, and ensures every worktree has working tooling.

When to use

  • "clean up / prune worktrees"
  • "create worktrees for my PRs"
  • "set up worktrees"
  • Periodic housekeeping after a batch of merges

Procedure

Run all steps in order. Each step feeds into the next.


Step 1 — Inventory

Collect current state in parallel:

# All worktrees (skip main — first line)
git worktree list

# All open PRs for current user
gh pr list --state open --author "@me" --json number,title,headRefName,url

# All merged PRs (to cross-reference stale worktrees)
gh pr list --state merged --author "@me" --json number,headRefName --limit 50

Derive MAIN path:

MAIN=$(git worktree list | head -1 | awk '{print $1}')

Step 2 — Identify stale worktrees

A worktree is stale (safe to remove) if either:

  • Its branch HEAD is an ancestor of main:
    git merge-base --is-ancestor <commit> main
    
  • Its associated PR is MERGED or CLOSED

Never remove a worktree whose PR is still OPEN.


Step 3 — Identify missing worktrees

A worktree is missing if an open PR's branch has no corresponding entry in git worktree list.


Step 4 — Report and confirm

Show one combined table before making any changes:

Branch Worktree path PR Action
feat/foo .data/worktrees/feat-foo #123 MERGED Remove
fix/bar (none) #456 OPEN Create
feat/baz .data/worktrees/feat-baz #789 OPEN Keep

Ask the user to confirm before proceeding.


Step 5 — Remove stale worktrees

git worktree remove --force <path>

Use --force — worktrees often have untracked files (build artifacts, .env). Offer to delete the local branch too:

git branch -d <branch>

Step 6 — Create missing worktrees

Use the worktree::create script which handles the full flow: checkout the branch, copy-on-write node_modules, and copy untracked config files.

Read the full file on GitHub · 193 lines

Files

What ships with it

1 file beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 4d ago First seen · 193 lines · 109 tokens per session scan A 354d487289ed

Subscribe to this mod's changes

worktree is a skill published in the GitHub repository DJRHails/dotfiles (2 stars, last pushed 3d ago), licensed MIT. It adds 109 tokens to every session and 1,718 once invoked, about $0.0005 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.