agentleFS
hdprajwalhdprajwal/agent-skills/skills/using-git-worktrees/SKILL.md

using-git-worktrees

Skill1 starsChanged 5 months agoMITView on GitHub

Make sure work happens in an isolated workspace before a feature or plan. A feature branch is the default and is enough for most work; reach for a worktree only when you need a second checkout at once (parallel agents, comparing branches, long build running). Detects existing isolation, prefers the harness's native worktree tool, falls back to git, verifies a clean baseline.

  • Installs packages

189 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use chinese-code-review, chinese-commit-conventions and 10 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.
  • Leodorareluctant259Skill

    chinese-code-review

    中文代码审查规范——在保持专业严谨的同时,用符合国内团队文化的方式给出有效反馈

    Also in 8 projects that use this

  • Leodorareluctant259Skill

    chinese-commit-conventions

    中文 Git 提交规范 — 适配国内团队的 commit message 规范和 changelog 自动化

    Also in 8 projects that use this

  • obraSkill

    diagnosing-superpowers

    Use when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.

    Also in 20 projects that use this

  • frankxaiSkill

    defense-in-depth

    Use when invalid data causes failures deep in execution, requiring validation at multiple system layers - validates at every layer data passes through to make bugs structurally impossible

    Also in 4 projects that use this

  • xfstudioSkill

    test-fixing

    Run tests and systematically fix all failing tests using smart error grouping. Use when user asks to fix failing tests, mentions test failures, runs test suite and failures occur, or requests to make tests pass.

    Also in 3 projects that use this

  • kntismSkill

    condition-based-waiting

    Use when tests have race conditions, timing dependencies, or inconsistent pass/fail behavior - replaces arbitrary timeouts with condition polling to wait for actual state changes, eliminating flaky tests from timing guesses

    Also in 3 projects that use this

  • +3 more from Leodorareluctant259

The file

We split this skill into the rules it gives an agent

6 rules. 0 are common, 4 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 182 edited versions we read keep these.

  • 4

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

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

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

    Native tool first: If the harness has a worktree tool (something like EnterWorktree, a /worktree command, or a --worktree flag), use it — …

    line 49
  2. Distinctive:

    If git worktree add fails on a sandbox permission error, tell the user and work in place.

    line 64
  3. Very specific:

    Run the test command (pnpm test / cargo test / go test ./...).

    line 81
  4. Distinctive:

    Start work on main/master.

    line 89
  5. Distinctive:

    Use git worktree add when a native worktree tool exists.

    line 91
  6. Distinctive:

    Create a project-local worktree without verifying it's ignored.

    line 92

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

Isolated Workspaces (Branches & Worktrees)

Work should never land directly on main. The default is a feature branch — that's what fits the usual loop (branch → fix → PR → merge → delete → next). A worktree is for when you genuinely need two checkouts at once: running parallel agents on independent tasks, comparing branches side by side, or keeping a long build/test going while you work elsewhere. If you don't need a second checkout, a branch is simpler — don't over-engineer it.

Say at the start: "Setting up an isolated workspace."

Branch (default)

git switch -c <feature-branch>     # never start on main/master

That's it for most work. Skip the rest unless you actually need a second checkout.

Worktree (when you need a second checkout)

Step 0 — detect existing isolation

GIT_DIR=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P)
GIT_COMMON=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P)

GIT_DIR != GIT_COMMON is also true inside a submodule, so guard:

git rev-parse --show-superproject-working-tree 2>/dev/null  # path = submodule
  • GIT_DIR != GIT_COMMON, not a submodule: already in a worktree — skip to Setup. Don't nest another.
  • Otherwise: normal checkout. If the user hasn't stated a preference, ask before creating one.

Step 1 — create it

Native tool first. If the harness has a worktree tool (something like EnterWorktree, a /worktree command, or a --worktree flag), use it — it handles placement, branch, and cleanup, and the harness can track it. Using git worktree add when a native tool exists creates phantom state it can't see.

Git fallback (only if no native tool). Default dir .worktrees/ (else worktrees/; if both, .worktrees/). Project-local dirs MUST be git-ignored first or their contents get committed:

git check-ignore -q .worktrees || { echo ".worktrees/" >> .gitignore && git add .gitignore && git commit -m "chore: ignore worktrees"; }
git worktree add ".worktrees/<branch>" -b "<branch>"
cd ".worktrees/<branch>"

If git worktree add fails on a sandbox permission error, tell the user and work in place.

Setup

Auto-detect and run:

[ -f pnpm-lock.yaml ]   && pnpm install
[ -f package.json ]     && [ ! -f pnpm-lock.yaml ] && npm install
[ -f Cargo.toml ]       && cargo build
[ -f go.mod ]           && go mod download
[ -f pyproject.toml ]   && (poetry install || pip install -e .)

Verify a clean baseline

Run the test command (pnpm test / cargo test / go test ./...).

  • Fail: report and ask whether to proceed — otherwise you can't tell new bugs from pre-existing ones.
  • Pass: report ready.

Never

  • Start work on main/master.
  • Spin up a worktree when a branch is all you need.
  • Use git worktree add when a native worktree tool exists.
  • Create a project-local worktree without verifying it's ignored.
  • Skip the baseline test, or push through failures without asking.

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.