using-git-worktrees
Skill1 starsChanged 2 months agoMITView on GitHub
Use when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback
- Installs packages
189 other projects use a version of this, 166 of them edited.
Adopted by
Most recently taken up by
- DemonDamon/AgenticXedited, first seen Nov 9, 2026
- chrysogonus/power-agentsedited, first seen Nov 8, 2026
- roundpilot/superpowers-antigravityedited, first seen Nov 8, 2026
- REPOZY/superpowers-optimizededited, first seen Nov 8, 2026
- lingyired/status-trioedited, first seen Nov 8, 2026
- afawfawffaf/my-skillsedited, first seen Nov 8, 2026
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.
Leodorareluctant259Skillchinese-code-review
中文代码审查规范——在保持专业严谨的同时,用符合国内团队文化的方式给出有效反馈
Also in 8 projects that use this
Leodorareluctant259Skillchinese-commit-conventions
中文 Git 提交规范 — 适配国内团队的 commit message 规范和 changelog 自动化
Also in 8 projects that use this
obraSkilldiagnosing-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
frankxaiSkilldefense-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
xfstudioSkilltest-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
kntismSkillcondition-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
Other versions189 projects use a version of this.
Word for word · 0
None found.
Edited · 166
- obra/superpowers★ 314k69% same
- jnMetaCode/superpowers-zh★ 8.4k19% same
- openai/plugins★ 7.8k69% same
- farm-fe/farm★ 5.6k77% same
- microsoft/FluidFramework★ 4.9k20% same
- lingyired/status-trio★ 1.8k69% same
- pcvelz/superpowers★ 1.3k63% same
- DemonDamon/AgenticX★ 1k32% same
- usewhale/DeepSeek-Code-Whale★ 93377% same
- usewhale/Whale★ 92877% same
156 more not listed
Wrote their own version · 23
- silvertakana/worldwideview★ 1.9ksame name
- GanyuanRan/Aegis★ 1.4ksame name
- Innei/ryugi★ 378same name
- KbWen/agentic-os★ 229same name
- KbWen/agent-virtual-office★ 65same name
- drvoss/everything-copilot-cli★ 48same name
- felvieira/claude-skills-fv★ 27same name
- RBraga01/a-team★ 24same name
- webpro/skills★ 18same name
- osmontero/opencode-skills★ 15same name
13 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository13 other files it gives its agents.
- AGENTS.mdAGENTS.md
- adversarial-reviewSkill
- architecture-guidelinesSkill
- auto-run-taskSkill
- code-auditingSkill
- commitSkill
- enrich-taskSkill
- harness-setupSkill
- run-taskSkill
- runtime-verificationSkill
- start-projectSkill
- update-docsSkill
- writing-skillsSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
37 rules. 1 is common, 24 are distinctive, and 12 are very specific to this project.
1
Common
Most of the 182 edited versions we read keep these.
24
Distinctive
This author's own choices. Worth a look.
12
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 13
Ensure work happens in an isolated workspace.
- Very specific: line 60
If the native flow does not propagate Claude/Cursor settings and the user expects parity with the main checkout, copy .claude/settings.json …
- Distinctive: line 62
Only proceed to Step 1b if you have no native worktree tool available.
- Very specific: line 70
Use a single location: .worktrees/ inside the repository root.
- Distinctive: line 78
Ensure the directory exists:
- Distinctive: line 83
Always create the worktree at:
- Distinctive: line 88
No global/sibling alternatives should be used unless the user explicitly overrides this rule for a one-off task.
- Very specific: line 98
If NOT ignored: Add .worktrees/ to .gitignore, commit the change, then proceed.
- Very specific: line 104
Capture the main checkout root before
git worktree add: After cd into the new worktree, git rev-parse --show-toplevel points at the … - Distinctive: line 117
Sandbox fallback: If git worktree add fails with a permission error (sandbox denial), tell the user the sandbox blocked worktree creation …
- Distinctive: line 121
After creating the worktree with git (Step 1b), copy local Claude settings from the main checkout so Cursor/Claude CLI behavior matches the …
- Distinctive: line 123
Do not copy if the user declines or if a native tool (Step 1a) already propagates settings—respect the harness.
- Very specific: line 125
Run after cd "$path" (still using SOURCE_ROOT captured above):
- Common: line 166
Run tests to ensure workspace starts clean:
- Distinctive: line 202
Before removing anything, confirm there is no work to lose:
- Distinctive: line 209
If either command returns output: Stop. Report the unsaved work to the user and ask how to proceed (commit/push, stash, or force discard).
- Very specific: line 220
If you used a native worktree tool in Step 1a (EnterWorktree, WorktreeCreate, /worktree, etc.), use the matching native command to remove …
- Distinctive: line 222
Only proceed to 5.3 if no native cleanup tool is available.
- Distinctive: line 248
Sandbox fallback: If removal fails due to permissions, report the failure and the path that needs manual cleanup.
- Distinctive: line 297
Fix: Always run Step 0 before creating anything
- Distinctive: line 302
Fix: Always use git check-ignore before creating project-local worktree
- Very specific: line 307
Fix: Always use <repo>/.worktrees/<branch> unless user explicitly overrides for one task
- Very specific: line 317
Fix: Always run git status --porcelain and git log @{u}..
- Distinctive: line 322
Fix: Prefer git branch -d (safe delete).
- Distinctive: line 337
Create a worktree when Step 0 detects existing isolation
- Very specific: line 338
Use git worktree add when you have a native worktree tool (e.g., EnterWorktree).
- Distinctive: line 340
Create worktree without verifying it's ignored (project-local)
- Distinctive: line 343
Remove a worktree that still has uncommitted, unpushed, or unmerged work without explicit user confirmation
- Distinctive: line 345
Run git worktree remove on a worktree created by a native tool
- Distinctive: line 348
Run Step 0 detection first
- Distinctive: line 349
Prefer native tools over git fallback
- Very specific: line 350
Use <repo>/.worktrees/<branch> as the standard location
- Distinctive: line 351
Verify directory is ignored for project-local
- Distinctive: line 353
Verify clean test baseline
- Distinctive: line 354
Run Step 5 cleanup once the work is done, using the same mechanism that created the worktree
- Very specific: line 355
Verify there is nothing to lose before removing a worktree (git status --porcelain, git log @{u}..)
- Very specific: line 357
After Step 1b (git fallback), copy .claude/settings.json and .claude/settings.local.json from SOURCE_ROOT captured before git worktree add
“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.
Files
Using Git Worktrees
Overview
Ensure work happens in an isolated workspace. Prefer your platform's native worktree tools. Fall back to manual git worktrees only when no native tool is available.
Core principle: Detect existing isolation first. Then use native tools. Then fall back to git. Never fight the harness.
Announce at start: "I'm using the using-git-worktrees skill to set up an isolated workspace."
Step 0: Detect Existing Isolation
Before creating anything, check if you are already in an isolated workspace.
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)
BRANCH=$(git branch --show-current)Submodule guard: GIT_DIR != GIT_COMMON is also true inside git submodules. Before concluding "already in a worktree," verify you are not in a submodule:
# If this returns a path, you're in a submodule, not a worktree — treat as normal repo
git rev-parse --show-superproject-working-tree 2>/dev/nullIf GIT_DIR != GIT_COMMON (and not a submodule): You are already in a linked worktree. Skip to Step 3 (Project Setup). Do NOT create another worktree.
Report with branch state:
- On a branch: "Already in isolated workspace at
<path>on branch<name>." - Detached HEAD: "Already in isolated workspace at
<path>(detached HEAD, externally managed). Branch creation needed at finish time."
If GIT_DIR == GIT_COMMON (or in a submodule): You are in a normal repo checkout.
Has the user already indicated their worktree preference in your instructions? If not, ask for consent before creating a worktree:
"Would you like me to set up an isolated worktree? It protects your current branch from changes."
Honor any existing declared preference without asking. If the user declines consent, work in place and skip to Step 3.
Step 1: Create Isolated Workspace
You have two mechanisms. Try them in this order.
1a. Native Worktree Tools (preferred)
The user has asked for an isolated workspace (Step 0 consent). Do you already have a way to create a worktree? It might be a tool with a name like EnterWorktree, WorktreeCreate, a /worktree command, or a --worktree flag. If you do, use it and skip to Step 3.
Native tools handle directory placement, branch creation, and cleanup automatically. Using git worktree add when you have a native tool creates phantom state your harness can't see or manage.
If the native flow does not propagate Claude/Cursor settings and the user expects parity with the main checkout, copy .claude/settings.json and .claude/settings.local.json from the primary workspace using the same loop as Step 1b after the native tool finishes—only when files exist on disk.
Only proceed to Step 1b if you have no native worktree tool available.
1b. Git Worktree Fallback
Only use this if Step 1a does not apply — you have no native worktree tool available. Create a worktree manually using git.
Directory Selection
Use a single location: .worktrees/ inside the repository root.
- Set the repository root and worktree base directory:
SOURCE_ROOT=$(git rev-parse --show-toplevel) LOCATION="$SOURCE_ROOT/.worktrees" - Ensure the directory exists:
mkdir -p "$LOCATION" - Always create the worktree at:
path="$LOCATION/$BRANCH_NAME"
No global/sibling alternatives should be used unless the user explicitly overrides this rule for a one-off task.
Safety Verification
MUST verify .worktrees/ is ignored before creating worktree:
git check-ignore -q .worktrees 2>/dev/nullIf NOT ignored: Add .worktrees/ to .gitignore, commit the change, then proceed.
Why critical: Prevents accidentally committing worktree contents to repository.
Create the Worktree
Capture the main checkout root before git worktree add. After cd into the new worktree, git rev-parse --show-toplevel points at the worktree directory, not the checkout where .claude/settings*.json usually lives.
project=$(basename "$(git rev-parse --show-toplevel)")
SOURCE_ROOT=$(git rev-parse --show-toplevel)
# Path is always inside repo-local .worktrees/
path="$LOCATION/$BRANCH_NAME"
git worktree add "$path" -b "$BRANCH_NAME"
cd "$path"Sandbox fallback: If git worktree add fails with a permission error (sandbox denial), tell the user the sandbox blocked worktree creation and you're working in the current directory instead. Then run setup and baseline tests in place.
Copy Claude configuration (Step 1b only)
After creating the worktree with git (Step 1b), copy local Claude settings from the main checkout so Cursor/Claude CLI behavior matches the primary workspace. These files are often untracked.
Do not copy if the user declines or if a native tool (Step 1a) already propagates settings—respect the harness.
Run after cd "$path" (still using SOURCE_ROOT captured above):
copied_claude_settings=false
for claude_settings in ".claude/settings.json" ".claude/settings.local.json"; do
if [ -f "$SOURCE_ROOT/$claude_settings" ]; then
mkdir -p ".claude"
cp -p "$SOURCE_ROOT/$claude_settings" "./$claude_settings"
echo "Copied $claude_settings to worktree"
copied_claude_settings=true
fi
done
if [ "$copied_claude_settings" = false ]; then
echo "No local Claude settings found (.claude/settings.json or .claude/settings.local.json)"
fiSymlinks: .claude/skills/<name> points at ../../.agents/skills/<name>, a repository-relative symlink, so a worktree resolves it on its own. A symlink that escapes the repository (into $HOME, say) does not survive a clone on another machine and should not be committed - the harness vendors skills as real directories under .agents/skills for exactly that reason.
Step 3: Project Setup
Auto-detect and run appropriate setup:
# Node.js
if [ -f package.json ]; then npm install; fi
# Rust
if [ -f Cargo.toml ]; then cargo build; fi
# Python
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f pyproject.toml ]; then poetry install; fi
# Go
if [ -f go.mod ]; then go mod download; fiStep 4: Verify Clean Baseline
Run tests to ensure workspace starts clean:
# Use project-appropriate command
npm test / cargo test / pytest / go test ./...If tests fail: Report failures, ask whether to proceed or investigate.
If tests pass: Report ready.
Report
Worktree ready at <full-path>
Claude settings copied (or none found / skipped per harness)
Tests passing (<N> tests, 0 failures)
Ready to implement <feature-name>Step 5: Cleanup — Remove the Worktree When Done
Run cleanup once the work is complete. "Done" means: branch merged, PR closed, the experiment was discarded, or the user has explicitly confirmed they no longer need the isolated workspace. Never remove a worktree that still contains uncommitted, unpushed, or unmerged changes the user may want.
5.0 Detect Cleanup Mode
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)- If
GIT_DIR == GIT_COMMON: You were never in a linked worktree (Step 0 sent you straight to Step 3). There is nothing to clean up — skip Step 5 entirely. - If
GIT_DIR != GIT_COMMON: You are inside a linked worktree. Continue with cleanup.
5.1 Verify Work Is Saved
Before removing anything, confirm there is no work to lose:
git status --porcelain # Must be empty (no uncommitted changes)
git log @{u}.. 2>/dev/null # Must be empty (no unpushed commits)If either command returns output: Stop. Report the unsaved work to the user and ask how to proceed (commit/push, stash, or force discard). Never delete a worktree with unsaved work without explicit user confirmation.
Capture the worktree path and branch name before leaving the directory:
WORKTREE_PATH=$(git rev-parse --show-toplevel)
BRANCH_NAME=$(git branch --show-current)5.2 Native Worktree Tools (preferred)
If you used a native worktree tool in Step 1a (EnterWorktree, WorktreeCreate, /worktree, etc.), use the matching native command to remove the worktree (e.g. LeaveWorktree, WorktreeRemove, /worktree remove). Native tools clean up directories, branches, and harness state consistently.
Only proceed to 5.3 if no native cleanup tool is available.
5.3 Git Worktree Fallback Cleanup
Only use this if Step 5.2 does not apply — you created the worktree manually in Step 1b.
# 1. Move out of the worktree before deleting it
cd "$GIT_COMMON/.." # or any path inside the main checkout
# 2. Remove the worktree
git worktree remove "$WORKTREE_PATH"
# 3. If files are still present (e.g. ignored artifacts blocking removal),
# confirm with the user, then force-remove
git worktree remove --force "$WORKTREE_PATH"
# 4. Delete the local branch only if it was created for this worktree
# AND has been merged or is no longer needed
git branch -d "$BRANCH_NAME" # safe delete (refuses if unmerged)
git branch -D "$BRANCH_NAME" # force delete (only with user confirmation)
# 5. Prune stale worktree metadata if the directory was deleted manually
git worktree pruneSandbox fallback: If removal fails due to permissions, report the failure and the path that needs manual cleanup. Do not retry destructively.
5.4 Verify Cleanup
git worktree list # WORKTREE_PATH must no longer appear
ls -d "$WORKTREE_PATH" 2>/dev/null # Must return nothingReport
Worktree removed: <full-path>
Branch <name> deleted (or kept, if still needed)
Main checkout left untouchedQuick Reference
| Situation | Action |
| Already in linked worktree | Skip creation (Step 0) |
| In a submodule | Treat as normal repo (Step 0 guard) |
| Native worktree tool available | Use it (Step 1a) |
| No native tool | Git worktree fallback (Step 1b) |
| Need a worktree location | Use <repo>/.worktrees/<branch> |
| Directory not ignored | Add to .gitignore + commit |
| Permission error on create | Sandbox fallback, work in place |
| Tests fail during baseline | Report failures + ask |
| No package.json/Cargo.toml | Skip dependency install |
| Work complete, in linked worktree | Run Step 5 cleanup |
| Never created a worktree | Skip Step 5 |
| Uncommitted/unpushed changes at cleanup | Stop and ask user |
| Native cleanup tool available | Use it (Step 5.2) |
| No native cleanup tool | git worktree remove (Step 5.3) |
| Worktree directory deleted manually | git worktree prune |
| Git fallback: clone Claude behavior into worktree | Copy .claude/settings*.json from SOURCE_ROOT (Step 1b) |
Repo uses .claude symlinks | Copy JSON only is not enough—recreate symlinks or paths |
Common Mistakes
Fighting the harness
- Problem: Using
git worktree addwhen the platform already provides isolation - Fix: Step 0 detects existing isolation. Step 1a defers to native tools.
Skipping detection
- Problem: Creating a nested worktree inside an existing one
- Fix: Always run Step 0 before creating anything
Skipping ignore verification
- Problem: Worktree contents get tracked, pollute git status
- Fix: Always use
git check-ignorebefore creating project-local worktree
Assuming directory location
- Problem: Creates inconsistency, violates project conventions
- Fix: Always use
<repo>/.worktrees/<branch>unless user explicitly overrides for one task
Proceeding with failing tests
- Problem: Can't distinguish new bugs from pre-existing issues
- Fix: Report failures, get explicit permission to proceed
Removing a worktree with unsaved work
- Problem:
git worktree remove --forcediscards uncommitted/unpushed changes silently - Fix: Always run
git status --porcelainandgit log @{u}..before removal; stop and ask if either is non-empty
Deleting the branch before it is merged
- Problem:
git branch -Ddestroys commits that were never merged or pushed - Fix: Prefer
git branch -d(safe delete); only force-delete after explicit user confirmation
Mixing native and manual cleanup
- Problem: Running
git worktree removeon a worktree created by a native tool leaves phantom harness state - Fix: Whatever created the worktree must remove it (native tool ↔ native tool, git ↔ git)
Copying Claude settings from the wrong directory
- Problem: Using
git rev-parse --show-toplevelaftercdinto the new worktree resolves to the worktree path, so copies find no settings or copy from the wrong place - Fix: Set
SOURCE_ROOT=$(git rev-parse --show-toplevel)in the main checkout beforegit worktree add, then use$SOURCE_ROOTin the copy loop
Red Flags
Never:
- Create a worktree when Step 0 detects existing isolation
- Use
git worktree addwhen you have a native worktree tool (e.g.,EnterWorktree). This is the #1 mistake — if you have it, use it. - Skip Step 1a by jumping straight to Step 1b's git commands
- Create worktree without verifying it's ignored (project-local)
- Skip baseline test verification
- Proceed with failing tests without asking
- Remove a worktree that still has uncommitted, unpushed, or unmerged work without explicit user confirmation
- Force-delete a branch (
git branch -D) without confirming it is merged or no longer needed - Run
git worktree removeon a worktree created by a native tool
Always:
- Run Step 0 detection first
- Prefer native tools over git fallback
- Use
<repo>/.worktrees/<branch>as the standard location - Verify directory is ignored for project-local
- Auto-detect and run project setup
- Verify clean test baseline
- Run Step 5 cleanup once the work is done, using the same mechanism that created the worktree
- Verify there is nothing to lose before removing a worktree (
git status --porcelain,git log @{u}..) - Confirm with
git worktree listand a directory check that cleanup actually completed - After Step 1b (git fallback), copy
.claude/settings.jsonand.claude/settings.local.jsonfromSOURCE_ROOTcaptured beforegit worktree add
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
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.

