Skip to content
Skill

Main bash cwd persists nested worktree

Stops git worktrees from landing in nested paths when the main agent's shell cwd drifts

Main bash cwd persists nested worktree is an open-source agent skill for developer tools. It is available on Claude Code.

Scripts
None
Platforms
Claude Code
GitHub stars
5
Last commit
License
MIT
Listing updated

Similar skills

About Main bash cwd persists nested worktree

This skill documents a failure mode in the main Claude Code agent's Bash tool, where a working directory changed by an earlier cd carries into later calls. A relative git worktree add then creates worktrees at unexpected nested or parent-directory locations, and file tools may report files as missing even though they exist elsewhere.

The guidance covers prevention and diagnosis: pass absolute paths to git worktree add, chain a cd to the project root before relative paths, create all worktrees in one batch early in a session, and check git worktree list to confirm the real location. It also covers a related symptom where a pathspec-filtered git diff returns empty while an unfiltered --stat shows the change, which points to cwd drift rather than the commit itself.

It is written as a Markdown reference for an agent to follow, with shell command examples and a suggested verification step to include in parallel subagent prompts. It expects a git repository, the git CLI, and the Claude Code Bash tool.

When your agent uses it

Prevent and diagnose nested git worktrees created at the wrong filesystem path when orchestrating parallel work from the main Claude Code agent. Use when (1) you ran `git worktree add .claude/worktrees/<name> ...` from the main agent's Bash tool, (2) `git worktree list` shows the new worktree at an unexpected nested path like `.claude/worktrees/<previous-worktree>/.claude/worktrees/<name>` instead of the intended top-level location, (3) a subagent dispatched to the intended path reports "worktree path mismatch" or operates from a longer nested path. Root cause: the main agent's Bash tool **persists cwd between calls** (per its docstring: "The working directory persists between commands"). An earlier `cd /abs/path && cmd` changes the main shell's cwd; a later `git worktree add <relative-path> ...` resolves against that persisted cwd rather than the project root, silently creating nested layouts. This is the **inverse** of the subagent variant (subagent-bash-cd-wrong-worktree) — subagent shells reset per call,

From the skill's SKILL.md. Your agent reads this description to decide when to load the skill.

What it can access

From a static check of its 1 file (13 KB) on GitHub on 4 December 2026. Nothing was run. Read the files before you install a skill: it can tell your agent to run anything its tools allow.

Check another skill before you install it.

Install

This skill ships in the agent-traffic-control plugin. In Claude Code, add the marketplace and install the plugin (it may include other skills too):

/plugin marketplace add wan-huiyan/agent-traffic-control
/plugin install agent-traffic-control@wan-huiyan-agent-traffic-control

Or copy only this skill to ~/.claude/skills/main-bash-cwd-persists-nested-worktree:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git "$tmp" &&
mkdir -p ~/.claude/skills &&
rm -rf ~/.claude/skills/main-bash-cwd-persists-nested-worktree &&
cp -R "$tmp/plugins/agent-traffic-control/skills/main-bash-cwd-persists-nested-worktree" ~/.claude/skills/main-bash-cwd-persists-nested-worktree &&
rm -rf "$tmp"

For one project only, put the folder in .claude/skills/main-bash-cwd-persists-nested-worktree inside it instead.

Claude Code docs on skills

Copy the skill folder to ~/.agents/skills/main-bash-cwd-persists-nested-worktree to use it in every repository:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git "$tmp" &&
mkdir -p ~/.agents/skills &&
rm -rf ~/.agents/skills/main-bash-cwd-persists-nested-worktree &&
cp -R "$tmp/plugins/agent-traffic-control/skills/main-bash-cwd-persists-nested-worktree" ~/.agents/skills/main-bash-cwd-persists-nested-worktree &&
rm -rf "$tmp"

For one repository only, put the folder in .agents/skills/main-bash-cwd-persists-nested-worktree inside it instead.

Codex docs on skills

Copy the skill folder to ~/.cursor/skills/main-bash-cwd-persists-nested-worktree to use it in every project:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git "$tmp" &&
mkdir -p ~/.cursor/skills &&
rm -rf ~/.cursor/skills/main-bash-cwd-persists-nested-worktree &&
cp -R "$tmp/plugins/agent-traffic-control/skills/main-bash-cwd-persists-nested-worktree" ~/.cursor/skills/main-bash-cwd-persists-nested-worktree &&
rm -rf "$tmp"

For one project only, put the folder in .cursor/skills/main-bash-cwd-persists-nested-worktree inside it instead.

Cursor docs on skills

Run in a terminal (add --scope workspace to install it for the current workspace only):

gemini skills install https://github.com/wan-huiyan/agent-traffic-control.git --path plugins/agent-traffic-control/skills/main-bash-cwd-persists-nested-worktree

Or copy the folder to ~/.gemini/skills/main-bash-cwd-persists-nested-worktree yourself.

Gemini CLI docs on skills

For GitHub Copilot in VS Code, copy the skill folder to ~/.copilot/skills/main-bash-cwd-persists-nested-worktree:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git "$tmp" &&
mkdir -p ~/.copilot/skills &&
rm -rf ~/.copilot/skills/main-bash-cwd-persists-nested-worktree &&
cp -R "$tmp/plugins/agent-traffic-control/skills/main-bash-cwd-persists-nested-worktree" ~/.copilot/skills/main-bash-cwd-persists-nested-worktree &&
rm -rf "$tmp"

For one project only, put the folder in .github/skills/main-bash-cwd-persists-nested-worktree inside it instead.

VS Code docs on skills

Get the folder:

git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git

Then copy agent-traffic-control/plugins/agent-traffic-control/skills/main-bash-cwd-persists-nested-worktree into your agent's skills folder as main-bash-cwd-persists-nested-worktree. Agents that support SKILL.md read one folder per skill.

The SKILL.md format

What it does

Nested worktree detection
Shows how to spot a worktree created inside another worktree by reading git worktree list output.
Absolute path guidance
Recommends passing absolute paths or resetting to the project root before creating worktrees.
Parent directory escape
Explains how a ../ relative path places a worktree outside the repository convention.
Pathspec drift diagnosis
Describes how an empty filtered git diff alongside a non-empty stat points to shell cwd drift.
Subagent path verification
Provides a pwd and branch check to include at the start of parallel subagent prompts.

Good for

  • Create several git worktrees for parallel feature branches without nesting them
  • Find out why a worktree is listed at an unexpected path
  • Fix a subagent that reports a worktree path mismatch
  • Inspect a commit's diff when a filtered git diff comes back empty

Main bash cwd persists nested worktree FAQ

What triggers Main bash cwd persists nested worktree?

It applies when a git worktree was added from the main agent's Bash tool, git worktree list shows an unexpected nested path, or a subagent reports a worktree path mismatch.

What is the root cause described?

The main agent's Bash shell keeps its working directory between calls, so a relative git worktree add path can resolve against a previously changed directory instead of the project root.

Does the skill run scripts or need extra files?

No. It is a single SKILL.md with no scripts or reference files, so it relies on the agent running the documented git commands.

Does the skill cover subagent shells too?

It references a related skill for the opposite case in subagent shells, where the working directory resets each call.

How do I install Main bash cwd persists nested worktree in Claude Code?

Copy the skill folder from GitHub to ~/.claude/skills/main-bash-cwd-persists-nested-worktree to use it in all your projects, or to .claude/skills/main-bash-cwd-persists-nested-worktree in one project. Or install the agent-traffic-control plugin, which includes it: /plugin marketplace add wan-huiyan/agent-traffic-control, then /plugin install agent-traffic-control@wan-huiyan-agent-traffic-control.

Does Main bash cwd persists nested worktree run scripts?

No. It is instructions only, with no scripts. This comes from a static check of the files on GitHub; nothing was run. Read the files before you install it.

Which AI agents can use Main bash cwd persists nested worktree?

Claude Code. It reads skills in the SKILL.md format; the install steps on this page show where to put the folder.

What are the best alternatives to Main bash cwd persists nested worktree?

The closest alternatives to Main bash cwd persists nested worktree are Harness read write base repo path in worktree stale tree, Git worktree, Dispatched bash agent git checkout clobbers uncommitted edit, Isolate subagent verification from live worktree and Wt.

Where this comes from

  • Description: from the skill's own SKILL.md.

More from wan-huiyan/agent-traffic-control