# My coding agent edited the wrong branch or worktree. What now?

> Identify the actual checkout, preserve every change, move the intended work safely, and verify the correct branch before restarting the agent.

Canonical HTML: https://canopyide.dev/guides/coding-agent-edited-wrong-branch-or-worktree
Article date: 2026-09-28

An agent's final message may name the branch you asked for while its process wrote to another directory. A Git worktree gives each branch separate files, but a shell command with an absolute path, a reused terminal, or a service started from an older checkout can still point at the wrong place. Stop new edits, identify the actual filesystem state, and move only the work you understand.

## Pause the writer and inventory every checkout

Ask the agent to stop editing while you inspect. In each suspected worktree, run Git commands from that directory to print the top-level path, branch name, short status, and recent diff summary. Use git worktree list to map branches to directories. Canopy's current-main README says Agent Workspace joins sessions to checkouts and branch state, but check the real Git output as well; CLI integration and file attribution differ. Do not switch branches, reset, clean, or remove a worktree while there are unexplained changes.

- Record the agent session, terminal working directory, checkout path, branch, and running service directory.
- Look for untracked files as well as modified tracked files; a diff alone omits untracked work.
- Check the main checkout too, even if the agent was launched for an isolated worktree.

## Find where the edit actually landed

The repository path from git rev-parse --show-toplevel answers which worktree a command is in. git branch --show-current identifies the checked-out branch, and git status --short shows working-tree and index changes. If a task made commits, inspect git log and the commit's file list; an empty working tree does not prove the task did nothing. Compare the changed files with the original task and with other active agents before deciding ownership. An agent file list can be incomplete for some CLIs, so inventory Git status, untracked files, commits, and the diff rather than trusting that list alone.

## Preserve the accidental work before relocating it

If there are useful uncommitted changes in the wrong checkout, first save an inspectable checkpoint on a clearly named recovery branch or copy the specific patch through your team's reviewed Git workflow. Include untracked files intentionally; a normal git diff does not include them. If the work is mixed with another person's edits, do not make one blanket commit or stash and then assume attribution. Separate files or hunks by intent with the owner present, and review the resulting patch. When a wrong-branch commit exists, cherry-pick only the reviewed commit onto the intended branch and inspect conflicts and tests. Avoid force pushes and hard resets as a shortcut to a clean screen.

- Keep the original checkout unchanged until the intended branch builds and passes its acceptance checks.
- Write down the source and destination commit IDs when moving committed work.
- Review the destination diff for unrelated files before opening or updating a PR.

## Fix the process and preview mapping

Restart the agent in the intended worktree, then ask it to print its current directory and branch before any further edit. If Canopy has a saved run command or detected URL, confirm that service starts in the same component and checkout; a page served from an old process can make a correct code change appear absent. Use a distinct port when two worktrees run the same app. The current-main Canopy README describes branch tracking and workspace port allocation, but exact controls may differ in a downloadable release.

## Close with a branch-level acceptance check

In the destination worktree, run the relevant tests and inspect the running behavior. Then compare the destination branch against its base, inspect all changed and untracked files, and confirm the wrong checkout no longer contains unexplained task work. For a PR, verify its head branch and latest commit on GitHub before requesting review. A corrected terminal title is not evidence that the right code reached the right branch.

## Copyable resources

### Read-only checkout inventory

Run from each suspected worktree before moving or deleting anything.

````text
pwd
git rev-parse --show-toplevel
git branch --show-current
git status --short
git diff --stat
git diff --cached --stat
git log -1 --oneline
git worktree list
# Repeat in the main checkout and every agent worktree.
# List untracked files from git status; they are absent from git diff.
````

## Frequently asked questions

### Does a worktree prevent an agent from writing an absolute path in another checkout?

No. Worktrees separate directories and indexes; a process with filesystem access can still target another path. Verify the process working directory and final Git state.

### Why is git status clean when the agent says it changed code?

You may be in a different worktree, the agent may have committed, or its claim may be wrong. Check every checkout and recent commits before assuming the work is gone.

### Can I move a mixed set of changes with one stash?

A stash can preserve work, but a mixed checkout still needs attribution and review. Separate the intended patch before applying it elsewhere; include untracked files deliberately.

## Sources and further reading

- [Git worktree documentation: linked checkouts and listing](https://git-scm.com/docs/git-worktree)
- [Git rev-parse documentation: show-toplevel](https://git-scm.com/docs/git-rev-parse)
- [Canopy README: worktrees, branches, and Agent Workspace](https://github.com/FluidWorksApp/canopy-ide/blob/main/README.md)
- [Canopy agent parity audit: file attribution by CLI](https://github.com/FluidWorksApp/canopy-ide/blob/main/docs/agent-parity.md)

## Related Canopy pages

- [Git worktrees for parallel coding agents: commands and cleanup](https://canopyide.dev/guides/git-worktrees-for-parallel-coding-agents.md)
- [How do I stop two agent worktrees from using the same port?](https://canopyide.dev/use-cases/avoid-dev-server-port-collisions-with-worktrees.md)
- [How do I resolve a merge conflict between two coding agents?](https://canopyide.dev/guides/resolve-merge-conflict-between-coding-agents.md)
- [Which coding-agent CLIs work in Canopy, and what differs?](https://canopyide.dev/guides/coding-agent-cli-support-in-canopy.md)
- [Agent session, terminal tab, branch, or worktree: what is the difference?](https://canopyide.dev/guides/agent-session-vs-tab-vs-worktree.md)

Canopy runs installed coding CLIs; CLI accounts, model selection, and provider billing remain separate. Check the installed release before relying on version-specific behavior.
