/simgit:simgit-worktreesfrom simgit
Manages disposable Git worktrees for autonomous-agent, parallel, or benchmark tasks. Handles allocation, cleanup, and detached or branch-attached workspaces.
Ships in simgit. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
simgit is the canonical executable; sg is its equivalent alias. This skill describes safe use, but does not replace an allocator/provider hook. A capable harness must perform preflight, allocation, and cleanup, pass the cleanup token unchanged, and start the worker with its current directory set to the returned path.
One disposable workspace for one job is the common case, and this is the whole sequence. Each step names the section that owns its rules; read that section for anything the recipe does not cover.
# 1. Discover and verify — see "Discover and verify the executable"
SIMGIT=$(command -v simgit || command -v sg) || exit 1
case "$SIMGIT" in
/*) ;;
*/*) SIMGIT="$(cd -P "${SIMGIT%/*}" && pwd -P)/${SIMGIT##*/}" ;;
*) echo "simgit discovery did not return an executable path" >&2; exit 1 ;;
esac
[ -f "$SIMGIT" ] && [ -x "$SIMGIT" ] || exit 1
"$SIMGIT" doctor --json # require top-level "identity": "simgit" and "version" >= 0.5.0 — see "Preflight"
# 2. Preflight, run again from the source repository — see "Preflight"
"$SIMGIT" doctor --json # require git_worktree_supported; read cow_supported, repository owner,
# populate_mode, stale_worktree_registrations, baseline_cache; choose an
# explicit absolute allocation root outside the source repository
# 3. Allocate an ephemeral workspace — see "Allocate with JSON"
"$SIMGIT" add --json --ephemeral --path <fresh-absolute-path-outside-source>
# read: path — the cwd to launch in; worktree — same absolute path; cleanup_token — retain unchanged;
# branch — the generated agent/<uuid>, record it; mode — cow-clone, overlay, or git-checkout
# 4. Launch the worker with its cwd set to path; it inspects, edits, commits, and integrates with
# ordinary git — see "Integrate and clean up"
# 5. Clean up this job only — see "Integrate and clean up"
"$SIMGIT" remove --json "$cleanup_token" # add --delete-branch while the token still resolves,
# once that branch is merged
A nonzero exit is the only failure signal: stdout is empty and stderr carries a one-line reason. Report that text verbatim; never retry a refusal by adding a destructive flag.
Leave the fast path for the cases that need judgement:
--attach <branch>, in Allocate with JSON.--detach --base <commit>, in Allocate with JSON.git-checkout fallback would invalidate — --require-cow, in Allocate with JSON.--discard-dirty and --delete-unmerged are never added automatically.run lock — simgit unlock, in Integrate and clean up.simgit prune --all --json, in Integrate and clean up; cache_busy: true or an "in use" refusal means retry later.command -v simgit; only if it is absent, resolve command -v sg../simgit or bin/simgit for relative PATH entries; make that path
absolute before changing directories, as in the fast path above.doctor --json command. It needs no repository and can be run from any directory.identity field to be exactly simgit; reject a missing, invalid, or mismatched identity. (product also reads simgit, but identity is the field this contract is written against.) Do not trust a filename, banner, or unrelated command found earlier on PATH.simgit means this path, including when discovery selected the alias.If no candidate passes that identity check, follow Install safely below rather than silently changing the user's environment. That includes the common case where simgit is absent and the sg on PATH is a different tool — ast-grep ships an sg whose sg --version output is deceptively close to simgit's. A command that exists but fails the identity check is not a fallback; treat it as "no simgit installed".
doctor --json answers two separate questions; check them in order.
First, identity and machine capability, which do not depend on a repository and can be verified from any directory:
cow_supported is true or false when simgit could write a probe there to find out, and null when it could not write one at all. null describes the directory, not the filesystem, and is not a reason to stop; re-run doctor from a writable directory to get a verdict.Second, repository readiness, reported when doctor runs inside the source repository:
Outside a Git worktree those repository fields are null and the stale-registration list is empty, while identity, version, filesystem, and CoW support are still reported. A null repository means "not in a repository here"; it is never evidence that the executable is wrong or broken, and the harness must not reject the binary for it.
Stop on an identity mismatch, unsupported Git worktrees, or an ownership problem. Treat stale registrations and retained baselines as state to review, not permission to delete work. Never allocate inside the source repository: override an unsafe default and choose an explicit absolute allocation root outside the source repository.
This skill describes simgit 0.5.0. Compare doctor --json's version with it before relying on anything below:
remove and gc do not protect a detached worktree whose commits no ref reaches (there is no unreached-commits), so removing one can leave those commits dangling. Older than 0.4.0, add --attach, the per-branch allocation claim, and prune's cache_busy field do not exist either, and prune may delete a baseline an allocation is using. Tell the user, and either get approval to upgrade (see Install safely) or limit yourself to what the installed version documents in its --help; for an existing branch, simgit run <branch> -- <command> works on older versions.npx skills update simgit-worktrees -g, or a fresh copy for a manual install) before depending on behavior it does not describe.In Claude Code with the simgit plugin active, the harness allocates and removes worktrees through simgit hook, so the agent does not run simgit for its own infrastructure and works inside the directory it is given. In Codex the same plugin brings this skill, but its hooks never allocate: when the work needs isolation, the agent uses the fast path above itself, runs its commands with the returned path as the working directory, and cleans up with the cleanup_token.
Always request machine-readable output with the global --json flag and parse it; do not scrape human output. Success is one JSON object on stdout. Failures are not JSON: the command exits nonzero, writes nothing to stdout, and puts a one-line reason on stderr. Branch on the exit status, and report the stderr text verbatim rather than trying to parse it.
--detach creates a collision-resistant agent/<uuid> branch.--ephemeral when the workspace is disposable.simgit add --json --attach <branch> --ephemeral --path <absolute-outside-source-path>. It checks the branch out at its current local commit without moving it; run git fetch origin <branch>:<branch> first to fast-forward it to the remote tip.simgit add --json --detach --base <commit> --ephemeral --path <absolute-outside-source-path>. This creates no branch.--require-cow only when falling back to a full checkout would invalidate the task, such as by violating its disk or scaling budget. Otherwise allow the safe fallback.--path is the worktree directory itself, not a parent to allocate inside: generate a fresh unique path per allocation. Missing parents are created, and an existing empty directory is accepted, so a harness that pre-creates one directory per job works; a non-empty directory is refused. Reusing one path for two concurrent allocations is never valid — exactly one of them is allowed to proceed. A path inside the source repository is not refused, only warned about on stderr, and that warning never appears in --json output: keeping allocations outside the source repository stays your responsibility.
add without --attach creates a branch and refuses one that already exists. For an existing branch use add --attach, which returns the same JSON record, or simgit run <branch> -- <command>, which reuses the branch's registered worktree (recreating one deleted by hand when dropping its registration loses nothing) but emits no JSON. Do not reach for --detach --base <branch>: commits on a detached HEAD never move the branch, so you would have to push by naming the remote branch and its old commit by hand, and the stale local branch keeps that old commit's baseline alive. --attach refuses a branch that is already checked out and names that worktree; work there instead of allocating another. Two allocations for one branch never both proceed: the second fails at once with "another simgit is already creating a worktree for branch", so wait for the first and use its worktree. Removing an attached worktree keeps the branch; delete it with --delete-branch only if the job owns it.
Require a successful JSON response. add --json retains the top-level worktree path string and also returns top-level provider fields path and cleanup_token. Verify that worktree, path, and cleanup_token identify the same absolute removable worktree. Require top-level branch to be a string for a branch worktree and null for a detached worktree. After creation, inspect top-level mode: cow-clone, overlay, or git-checkout. Reject an unexpected mode, and reject git-checkout only when the task truly required CoW.
The harness must retain cleanup_token, set the agent or benchmark process's cwd to path, and then launch it. Do not merely tell a model to allocate its own workspace from the source checkout.
The allocated directory is a normal linked Git worktree. Agents inspect, edit, commit, and integrate through ordinary Git; coordinators merge or rebase branches through normal Git workflows.
On completion, give the unchanged cleanup_token to the provider's safe cleanup
operation. Without a provider cleanup hook, run simgit remove "$cleanup_token";
the token is the absolute worktree path. Removal normally retains its branch.
Never append --discard-dirty or --delete-unmerged automatically.
Removal reclaims only what is inside the worktree. CoW shares the source tree,
but build output and caches (target/, node_modules, virtualenvs) are
written per worktree and are usually most of its disk use. Keep them inside the
worktree: a build pointed at a directory elsewhere, such as a per-job
CARGO_TARGET_DIR, outlives the worktree and is the job's to delete. On a
worktree that lives on, delete build output once it has served its purpose.
A worktree allocated without --ephemeral, such as one attached to a pull
request's branch for the length of its review, is never reaped by gc.
Remove it when the branch merges or closes; removal keeps the branch.
gc is a repository-wide sweep, not per-job cleanup: gc --older-than 0s can
reap a different, clean ephemeral workspace while its provider is launching
an agent there. Only run it in a separate maintenance pass that owns the
repository-wide cleanup decision. Pass --older-than <age> explicitly (default
24h); its skipped entries explain persistent, dirty, locked, recently
active, unreached-commits, status-failed (state unreadable) and
remove-failed worktrees. Never use a zero idle-age filter as an automatic follow-up
to one job's removal.
Delete a merged agent branch while its worktree still exists: simgit remove --json --delete-branch "$cleanup_token". That is a safe operation — it refuses an unmerged branch — but it only works while the token still resolves to a worktree. Once the worktree is gone the path cannot name a branch, so remove <absent-path> --delete-branch is an error; use the branch name recorded at allocation instead: simgit remove --json --delete-branch "$branch". Do this for every disposable job whose work was merged, or the generated agent/<uuid> branches accumulate in the repository permanently.
Removal and GC do not reclaim the baseline cache, which lives inside the
source repository at .git/simgit/baselines and holds one full tree per
distinct base commit. simgit prune --json also deregisters stale Git
worktree registrations and reports them in pruned_registrations; plain
prune drops a baseline unused for seven days, or one whose commit no ref or
detached worktree reaches any more. It never removes a baseline another job's
allocation is using and never waits for one: plain prune then keeps every
baseline it would have dropped and reports cache_busy: true, so run it again
later, and simgit prune --all --json fails instead, to be retried. Still run
cache pruning as repository maintenance rather than after every job, because
simgit prune --all --json drops every baseline now; those trees rematerialize
on future allocations, which then pay the full checkout cost. Confirm retained
disk use with doctor --json and
baseline_cache.retained_bytes.
Never pass --discard-dirty for dirty worktrees or in-progress merges/rebases, and never pass --delete-unmerged for unmerged branches, unless the user has explicitly authorized discarding that specific work. Neither flag implies the other, and --delete-unmerged applies only alongside --delete-branch (removal) or --delete-branches (GC). If safe cleanup refuses, report the retained path and branch instead of weakening the safety checks. When it refuses because the worktree is dirty, --commit -m "<message>" is the preserving alternative: it commits the leftover changes to the workspace's own branch and then removes the worktree, losing nothing. It still writes a commit on the agent's behalf, so ask before using it — but it is the option to offer instead of abandoning the workspace. A detached worktree has no branch to commit to, so there --commit is refused before it commits; offer git -C <path> switch -c <name> first, then --commit, as the error says.
A detached worktree (--detach) whose commits no branch or tag reaches is refused by remove (with or without --commit) and skipped by gc as unreached-commits, because removing it would leave those commits dangling. No flag overrides this and --commit does not help. Run git -C <path> branch <name> to keep the commits, then retry cleanup. If the worktree's directory is gone, run the git -C <main-checkout> branch <name> <commit> form the error names instead.
Cleanup is safe to run more than once. simgit remove on a target that no longer has a worktree exits successfully and reports already_absent: true, so an at-least-once cleanup path may retry without distinguishing "already removed" from "failed to remove". already_absent is the field to read; removed is present either way and is not evidence that anything was removed. It is the worktree's canonical resolved path when the target resolved to one, and the target string repeated back verbatim when nothing resolved. A target that still exists but cannot be safely removed continues to fail. A worktree whose directory was deleted by hand is already absent too: removal drops its registration. One whose registration holds work (staged changes, a merge or rebase in progress, submodule repositories or refs of its own) is refused as dirty, even when its submodule commits are pushed, so report the path rather than adding --discard-dirty. A locked one, or a detached one at unreached commits, is refused whatever the flags.
--commit removal has a seam worth knowing before you retry one: the commit happens first, and simgit never rolls it back if the removal then fails. Such a failure says which commit it kept — report that line verbatim — and repeating the identical command creates no second commit, because --commit only ever commits what the worktree still holds. The same is true of the directory a half-finished teardown leaves behind: an empty one is treated as already absent, while one that still holds files is refused rather than deleted.
If cleanup is refused because the workspace is locked by a simgit run whose launcher died, run simgit unlock <path> to clear the stranded lock, then retry removal. unlock succeeds on a workspace that is not locked and on a target that no longer exists at all, so it is safe to run unconditionally during recovery. It refuses while the PID recorded in the lock still answers kill(pid, 0), and two situations make that PID answer when a naive ps finds nothing useful: a killed launcher whose parent has not reaped it yet is a zombie and still answers, and a process the launcher spawned can outlive it in the same process group. So if ps on the named PID is empty or shows state Z and unlock still refuses, stop every surviving process in that launcher's process group (pkill -g <pid>; list them first with ps -eo pid,pgid,stat,command), let its parent reap it, and unlock again. There is no override flag; do not look for one. unlock clears only simgit's own locks: one set with git worktree lock is refused with Git's reason, and releasing it is the person's call (git worktree unlock <path>), not yours.
A killed add, or a run killed while still creating its workspace, may leave a registered worktree it never finished populating. That registration stays locked with the reason simgit: allocating (pid N), so safe removal first refuses it as locked; clear the lock with simgit unlock exactly as above. After that, the worktree shows every tracked file as deleted, so safe removal refuses it as dirty and simgit run refuses to reuse it. Its branch claim, path claim and baseline-cache lock are operating-system file locks released with the process. No agent ever ran there; report the path rather than adding --discard-dirty yourself.
Installation is a separate, approval-gated action:
PATH.SIMGIT_VERSION=vX.Y.Z, the installer verifies that the staged binary reports exactly simgit X.Y.Z before installing. Never automatically execute a curl-pipe-shell command.doctor --json to report the canonical simgit identity before use.simgit --json update --check only reports (its command field names how this install updates) and needs no approval; never run simgit update, brew upgrade simgit or a reinstall without the user's approval.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub abendrothj/simgit --plugin simgitOther plugins' skills for the same job