# Main bash cwd persists nested worktree

> Main bash cwd persists nested worktree is an open-source agent skill for developer tools. Stops git worktrees from landing in nested paths when the main agent's shell cwd drifts. It is available on Claude Code.

- **Kind:** Skill
- **Pricing:** Open source
- **Platforms:** Claude Code
- **Website:** https://github.com/wan-huiyan/agent-traffic-control/tree/main/plugins/agent-traffic-control/skills/main-bash-cwd-persists-nested-worktree
- **Repository:** https://github.com/wan-huiyan/agent-traffic-control
- **GitHub stars:** 5
- **License:** MIT
- **Skill name:** main-bash-cwd-persists-nested-worktree
- **Collection:** https://github.com/wan-huiyan/agent-traffic-control
- **Categories:** [developer tools](https://ai-powered.com/categories/developer-tools)
- **Listing updated:** 2026-12-04

## About

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,

## What it can access

From a static check of the files on GitHub; nothing was run.

- [Instructions only, no scripts](https://github.com/wan-huiyan/agent-traffic-control/tree/HEAD/plugins/agent-traffic-control/skills/main-bash-cwd-persists-nested-worktree)
- [License: MIT](https://github.com/wan-huiyan/agent-traffic-control/tree/HEAD/plugins/agent-traffic-control/skills/main-bash-cwd-persists-nested-worktree)

## Features

- **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

## Install

### Claude Code

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):

```sh
/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`:

```sh
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.

### Codex

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

```sh
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.

### Cursor

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

```sh
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.

### Gemini CLI

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

```sh
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.

### VS Code

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

```sh
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.

### Other agents

Get the folder:

```sh
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.

## 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.

## Alternatives

- [Harness read write base repo path in worktree stale tree](https://ai-powered.com/skills/harness-read-write-base-repo-path-in-worktree-stale-tree): Harness read write base repo path in worktree stale tree is an open-source agent skill for developer tools.
- [Git worktree](https://ai-powered.com/skills/git-worktree): Git worktree is an open-source agent skill for developer tools.
- [Dispatched bash agent git checkout clobbers uncommitted edit](https://ai-powered.com/skills/dispatched-bash-agent-git-checkout-clobbers-uncommitted-edit): Dispatched bash agent git checkout clobbers uncommitted edit is an open-source agent skill for developer tools.
- [Isolate subagent verification from live worktree](https://ai-powered.com/skills/isolate-subagent-verification-from-live-worktree): Isolate subagent verification from live worktree is an open-source agent skill for developer tools.
- [Wt](https://ai-powered.com/skills/wt-timvw): Wt is an open-source agent skill for developer tools.
- [Concurrent session checkout clobbers shared worktree](https://ai-powered.com/skills/concurrent-session-checkout-clobbers-shared-worktree): Concurrent session checkout clobbers shared worktree is an open-source agent skill for developer tools.
- [Injected claude md is the worktrees copy not mains](https://ai-powered.com/skills/injected-claude-md-is-the-worktrees-copy-not-mains): Injected claude md is the worktrees copy not mains is an open-source agent skill for developer tools.
- [Worktrees](https://ai-powered.com/skills/worktrees): Worktrees is an open-source agent skill for developer tools.

---
Source: https://ai-powered.com/skills/main-bash-cwd-persists-nested-worktree
