Skip to content
86% off$349 $48.86code BF2026

Guides

Claude Code Wrote Files to the Main Repo, Not the Worktree

Claude Code wrote files to the main repo instead of the worktree? Check the launch folder and stale paths, then move stray files back without losing work.

TroubleshootingUpdated 6 min read

On this page

Claude Code printed this, or something close to it:

I made a serious mistake - I wrote all files to the main repo path instead of the worktree path

New files sit untracked in the main checkout, and maybe an edit got swept into a commit you already pushed.

Why it happens

There are three causes, roughly in order of how often people report them.

  1. You started claude from the wrong folder. The folder you launch from anchors relative paths. Starting from the parent folder or the main checkout makes the agent snap to the main tree. A worktree is a separate folder that shares the repo's git data, so launching from the wrong one is easy.
  2. A stale absolute path is saved in memory or config. People on Anthropic's community forums report that Claude wrote a MEMORY.md with the main repo's path under "Working Directory", and later sessions followed it. A committed CLAUDE.md, a script or a config file with a hard-coded path can do the same.
  3. Wrong paths already in the conversation get reused. After the agent has produced /path/to/main-repo/src/... many times, it keeps producing it, even after you fix the cause.

One user said it happens "nearly every time I use a worktree for any length of time". Why it recurs is not confirmed, so treat the three causes above as the likely ones, not a proven diagnosis.

Check what landed in the main repo

Look at both checkouts yourself before asking the agent to fix anything. These commands only read.

  1. List every worktree and its path:

    bash
    git worktree list
  2. Check main for stray changes (use your path):

    bash
    git -C /path/to/main-repo status --short

    ?? lines are new files in the wrong place. M lines are edits to tracked files.

  3. Check whether anything was committed from main by mistake:

    bash
    git -C /path/to/main-repo log --stat -5

Step-by-step fixes

Fix 1: start Claude Code from inside the worktree

  1. Quit the current Claude Code session.

  2. Go into the worktree folder:

    bash
    cd /path/to/your-worktree
  3. Confirm where you are:

    bash
    pwd
    git rev-parse --show-toplevel
    git branch --show-current

    The first two must print the worktree path, not the main repo path. The branch must be the worktree's branch.

  4. Run claude from there.

If you create worktrees by hand, the whole routine is:

bash
git worktree add ../myrepo-feature -b feature
cd ../myrepo-feature
claude

The first line creates a folder next to your repo with a new branch. Remove it later with git worktree remove ../myrepo-feature.

Fix 2: remove absolute paths to the main repo

  1. Open any memory file the agent wrote and your CLAUDE.md files. Look for the main repo's path.

  2. Search both checkouts (replace the paths with yours):

    bash
    grep -rn "/path/to/main-repo" /path/to/main-repo /path/to/your-worktree --include="*.md" --include="*.json" --include="*.sh" --exclude-dir=node_modules --exclude-dir=.git
  3. Search Claude Code's per-user folder, ~/.claude by default, which can hold user-level CLAUDE.md and memory files. This lists matching file names only:

    bash
    grep -rl "/path/to/main-repo" ~/.claude --include="*.md"
  4. Replace each absolute path with a relative one, or delete the line. "Working directory: the folder you were started in" beats a fixed path, because a worktree never has the same path as main.

Fix 3: name the worktree path in the session

  1. Tell it directly: "Your working directory is /path/to/your-worktree. Write every file there. Never write under /path/to/main-repo."
  2. Ask it to run pwd and git rev-parse --show-toplevel and report the output before it edits anything.
  3. If it keeps drifting, end the session and start a fresh one from the worktree folder, then restate the path. A clean context has no old paths to repeat.

Fix 4: move the stray files into the worktree

Do this after fixes 1 to 3, so the agent doesn't write more while you clean up.

Uncommitted files and edits in main. If main's git status shows only the agent's strays and nothing of yours, stash them (untracked files included) and pop the stash in the worktree. Stashes are shared between worktrees:

bash
git -C /path/to/main-repo stash push -u -m "stray agent files"
git -C /path/to/your-worktree stash pop

If the pop hits conflicts or refuses to overwrite a file, git keeps the stash, so nothing is lost. Edits to tracked files apply cleanly only if the worktree's branch has similar contents. If main also holds your own work, move the stray files by hand with mv instead.

An edit already committed to main. If the stray commit is the latest and not pushed, git reset --soft HEAD~1 undoes it and leaves its changes staged. If it was pushed, don't rewrite shared history. Add a commit that restores the file:

bash
git show --stat <sha>
git checkout <sha>~1 -- path/to/file
git commit -m "Restore path/to/file"

If the commit added the file, <sha>~1 doesn't have it; run git rm path/to/file instead. Read git show --stat first. If the commit mixed the stray edit with real work, restore only the file that shouldn't be there, then redo the intended change in the worktree.

Cause, symptom and fix at a glance

CauseSymptomFix
Launched from parent or mainWrites go to main from the first promptFix 1
Stale path in memory or CLAUDE.mdRight folder, still writes to mainFix 2
Old paths in contextDrifts back mid-sessionFix 3, or a fresh session
Strays already in mainUntracked files, odd commitFix 4

Checklist before you try again

  • pwd and git rev-parse --show-toplevel print the worktree path.
  • No memory file, CLAUDE.md or script names the main repo's absolute path.
  • The session started fresh, or you restated the worktree path.
  • Main's git status is clean or only holds your own work.

Where Sledbot fits

We make Sledbot, a Mac app that runs coding agents such as Claude Code in real terminals. It removes the wrong-folder cause. It cannot fix a stale path inside the agent's own memory file, so Fixes 2 and 3 still apply. If you launch claude by hand from the right folder, you don't need an app.

  • The agent starts in the worktree. On the New task page, Work in a new worktree is on by default for git repos. The worktree goes to <repo>/.sledbot/worktrees/<branch name, with / replaced by -> and the agent starts there. Terminals in a worktree also get SLED_WORKTREE, SLED_ROOT and SLED_BRANCH. See Start an agent on a task.
  • Writes outside the worktree ask first. "File writes outside the worktree (temp folders excepted)" is a built-in ask rule. For Claude Code, Sledbot can block the action and show an approval card in Needs you, tagged "outside the worktree". A rule you saved earlier with Always allow can cover an action, so check Settings ▸ Approvals. Other CLI agents are only detect-and-answer. See Needs you and approvals.
  • Review shows what landed. Review changes compares the worktree with its base branch. If it says "This agent hasn’t changed anything compared with <base>.", look in the main repo. See Review an agent's changes.

Sledbot is part of MacSuite; you can try it free for 7 days (as of November 2026). See Sledbot, and for the manual setup Claude Code worktrees and running Claude Code agents in parallel.

If nothing works

If it still writes to main after you've checked the folder, memory files and context, report it through Anthropic's official Claude Code support channels. Include the output of git worktree list, pwd and git rev-parse --show-toplevel, where you launched claude, and whether a fresh session in the same worktree repeats it. A fresh session that behaves points to stale memory or context, not the launch folder.

text
Claude Code wrote files to the main repo path instead of the worktree.
Exact message: "I made a serious mistake - I wrote all files to the main repo path instead of the worktree path"
Launched from: <folder>
git worktree list: <output>
pwd and git rev-parse --show-toplevel in that folder: <output>
Memory/CLAUDE.md checked for absolute paths: yes/no
Fresh session in the worktree repeats it: yes/no

Common questions

Will git worktree stop Claude from touching main?

No. A worktree is just another folder. Nothing stops an agent from writing to any path it can reach, which is why the launch folder and stored paths matter.

Should I put the worktree path in CLAUDE.md?

No. The file is committed and shared by every worktree, so the path is wrong everywhere else. Use a relative description, or state the path per session.

Can I just ask Claude to move the files back?

You can, but check git status in both checkouts yourself first. An agent that already got the path wrong may repeat the mistake while fixing it.

Every app.
One purchase.