worktree

A Git worktree is a separate working folder connected to the same repository, usually with its own branch. It lets you develop a change away from the main branch.

In plain words
What is it for?
Starting isolated feature work, checking that the repository is clean, creating a branch and worktree, and running the existing tests before editing.
Why use it?
It keeps unfinished work and its files separate from the main branch, reducing accidental changes there.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/huzaifa525/claude-code-optimizer/worktree
Any agent
npx skills add huzaifa525/claude-code-optimizer --skill worktree
Clone the repo
git clone --depth 1 https://github.com/huzaifa525/claude-code-optimizer

Made for: Claude Code, Codex.

Per session 29 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 624 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
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 $0.00029 $0.00624
Opus 5 $0.00015 $0.00312
Sonnet 5 $0.00006 $0.00125
Haiku 4.5 $0.00003 $0.00062

Measured 2d ago against content hash 6955f8d02578, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, 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 2d ago.

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.

templates/.claude/skills/worktree/SKILL.md · 81 lines

How it starts

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

Create an isolated git worktree for safe development on: $ARGUMENTS

Iron Law

All new feature work happens in a worktree. The main branch stays clean.

If you think "this change is small enough to do on main" — you are wrong. Worktrees cost 5 seconds. Debugging a polluted main branch costs hours.

Steps

1. Verify Clean Baseline

Before creating the worktree:

# Ensure we're in a git repo
git rev-parse --is-inside-work-tree

# Check for uncommitted changes
git status --porcelain

If there are uncommitted changes, ask the user whether to stash or commit them first. Do NOT proceed with dirty state.

2. Create the Worktree

Sanitize the branch name from the user's description:

  • Lowercase everything
  • Replace spaces and special characters with hyphens
  • Remove consecutive hyphens
  • Example: "User Notifications!" → feature/user-notifications
# Create worktree on a new branch
git worktree add "../$(basename $PWD)-feature-$BRANCH_NAME" -b "feature/$BRANCH_NAME"

3. Verify Test Baseline

Run the project's test suite in the new worktree BEFORE making any changes:

cd "../$(basename $PWD)-$BRANCH_NAME"
# Run tests (detect test command from package.json, Makefile, etc.)

If tests fail BEFORE any changes, the baseline is broken. Stop and report.

4. Work in the Worktree

  • Make all changes in the worktree directory
  • Commit frequently with descriptive messages
  • Run tests after each meaningful change

5. Finish

When work is complete, present options:

Option Command When
Merge git checkout main && git merge $BRANCH_NAME Feature is complete and tested
PR gh pr create from the worktree branch Needs review before merge
Keep Leave worktree as-is Work in progress, will continue later
Discard git worktree remove ../worktree-dir Experiment failed, throw it away

Red Flags

  • "Let me just make this quick fix on main" — NO. Use a worktree.
  • "The tests were already failing" — Then fix the baseline FIRST.
  • "I'll clean up the branch later" — You won't. Clean it now.

Read the full file on GitHub · 81 lines

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. 2d ago First seen · 81 lines · 29 tokens per session scan A 6955f8d02578

Subscribe to this mod's changes

worktree is a skill published in the GitHub repository huzaifa525/claude-code-optimizer (9 stars, last pushed 5mo ago), licensed MIT. It adds 29 tokens to every session and 624 once invoked, about $0.0001 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-08-31.