skills/managing-git-worktrees/SKILL.md
Create, inspect, attach, repair, remove, prune, and clean up local Git worktrees safely. Use when Codex needs to manage a repository's worktree lifecycle, including creating a branch-backed worktree under worktrees/, attaching an existing local branch, repairing moved worktrees, diagnosing stale metadata, preserving detached commits and local-only files during removal, or optionally deleting a merged branch.
npx skillsauth add narumiruna/agent-skills managing-git-worktreesInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Inspect repository state before changing worktrees. Keep worktree operations separate from commit and push workflows.
Treat every branch, ref, and path as untrusted data. Prefer an argv-capable command interface; when using a shell, quote every dynamic value and never concatenate it into a command string. The variables in examples below stand for already validated values.
refs/heads/<invoking-branch> as the default start-point.git symbolic-ref --quiet --short HEAD fails during creation because the invoking HEAD is detached, stop and ask for an explicit start-point.git worktree list --porcelain -z; use its first record as the main worktree. Do not substitute git rev-parse --show-toplevel, which returns the invoking linked worktree.main.<main-worktree-root>/worktrees/<path-name>.<path-name> from the branch by replacing every / with -; for example, map feat/login to worktrees/feat-login.worktrees/ local by ensuring an exact /worktrees/ line appears in the common Git directory's info/exclude. Resolve the file with git rev-parse --path-format=absolute --git-path info/exclude, preserve all existing patterns, and append only when the exact line is absent. Do not overwrite the file or modify the tracked .gitignore.Validate and normalize the branch with git check-ref-format --branch "$branch"; use its output as the branch value and stop on failure.
Resolve the start-point to one commit with git rev-parse --verify --end-of-options "${start_point}^{commit}"; stop if it is missing or ambiguous.
Check for the local branch with git show-ref --verify --quiet "refs/heads/$branch".
Parse git worktree list --porcelain -z to determine branch occupancy. Do not use substring matching.
If the branch does not exist, create it from the resolved start object with:
git worktree add -b "$branch" "$worktree_path" "$start_oid"
If the branch exists and is not present in another worktree record, attach it with:
git worktree add "$worktree_path" "$branch"
If the branch is already checked out elsewhere, stop and report that worktree's path. Do not use --force to bypass branch occupancy.
Verify the exact path and branch with git worktree list --porcelain -z and git -C "$worktree_path" status --short --branch.
Parse git worktree list --porcelain -z. Confirm that the exact target path is registered and is not the first, main-worktree record.
Inventory tracked, untracked, ignored, and submodule state with:
git -C "$worktree_path" status --porcelain=v1 --untracked-files=all --ignored=matching --ignore-submodules=none
Treat every output line, including ?? and !!, as data that removal can discard. Expand directory entries when needed to make the loss concrete. If initialized submodules exist, inspect each recursively with the same status options. Stop and show the complete inventory before any destructive approval.
Read the target's porcelain record. If it is detached, record its HEAD object and run git for-each-ref --format='%(refname)' --contains="$head_oid" refs/heads refs/tags refs/remotes. If no shared durable ref contains it, stop and preserve it with a user-named branch or tag before removal.
If the target is locked, report its reason and stop. Unlock it only after the user explicitly confirms that the lock is no longer needed; do not bypass a lock with repeated --force.
If the inventory is empty and detached-commit reachability is safe, run git worktree remove "$worktree_path" and verify that its exact record disappeared.
If any local-only data exists, require explicit approval that names what will be lost. Use --force only when Git requires it and the user has approved the displayed loss; ignored files may be deleted even when Git does not require --force.
Preserve the branch by default.
git rev-parse --verify --end-of-options.git merge-base --is-ancestor "$branch_oid" "$comparison_oid".git branch -D "$branch" only after a separate, explicit request that acknowledges commits not contained in the comparison ref.refs/heads/$branch still points to $branch_oid, then try git branch -d "$branch". This command checks the branch's configured upstream, or current HEAD when no upstream exists, rather than the chosen comparison ref.-d refuses solely because those merge targets differ, report the mismatch. Use git branch -D "$branch" only after separate explicit approval to bypass Git's built-in check and after re-verifying that the branch still points to $branch_oid and remains an ancestor of $comparison_oid.git worktree repair inside a moved main or linked worktree; when repairing multiple moved linked worktrees from another worktree, pass each quoted new path to git worktree repair. Verify every repaired porcelain record.git for-each-ref --contains check and preserve any HEAD object not contained by a shared durable ref.git worktree prune --dry-run --verbose. Show the exact preview before running git worktree prune --verbose with the same expiry options.git worktree remove.git status as proof that removal is lossless.development
Score or compare one or more agent skills across trigger clarity, workflow actionability, safety boundaries, verification rigor, incremental knowledge value, and leanness. Use only when the user explicitly asks for ratings, numerical quality scores, rubric-based scorecards, or scored comparisons; use creating-agent-skills for unscored reviews or revisions.
development
Assess or improve an existing codebase's architecture when the user asks about module boundaries, coupling, scattered ownership, testability, change locality, deep modules, seams, or behavior-preserving structural refactoring. Use for cross-module design rather than ordinary diff review or a confirmed edge-case bug fix.
development
Perform read-only security audits, vulnerability assessments, or threat-focused reviews of diffs, pull requests, code paths, or explicitly scoped repositories when security is the primary objective or acceptance criterion. Use reviewing-code for ordinary review with baseline security coverage and hardening-code-paths for fixing confirmed findings.
development
Run iterative multi-reviewer panels over a code diff, verify their findings, apply explicitly authorized fixes, and re-review the updated change until it passes or reaches a stopping condition. Use when the user asks for a panel loop, multi-model code-review consensus, or a review-fix-re-review cycle.