← Blog
A floating cube over the violet arm of a glass fork, away from the cyan route.
Concept illustration of a change landing on the wrong branch.

What to do when an AI coding agent edits the wrong Git branch

Check which Git branch and worktree changed, preserve uncommitted edits, and recover safely after a local commit or push.

Published

You gave an AI coding agent a feature branch. It reports that the fix is ready, but the changed files are in your main checkout. Pause the agent, including any background workers, before asking it to fix the mistake. First find out where the edits landed and whether they left your machine. The answer determines what you can safely move without overwriting someone else’s work.

Check the folder, the branch and the changes separately

A branch is a name for a line of history. A checkout is the folder where you edit the files. A linked Git worktree gives you another working folder, with its own checked-out HEAD and staging area, but it still shares branch references and the object database with the original checkout. Opening a session in the right directory doesn’t prove that every edit or Git command stayed there.

That distinction mattered in Claude Code issue #95968. On September 22, 2026, a macOS user on version 2.1.274 reported that a session started in a linked worktree modified the main checkout instead when asked to rebase. The report was still open and marked stale on November 28, with no linked fix in the issue. This is one user’s report, not a reproduction or a claim about other versions.

From each suspected folder, run these read-only checks yourself, or use git -C <folder> before each subcommand:

Terminal window
git worktree list
git rev-parse --show-toplevel
git branch --show-current
git rev-parse --short HEAD
git status --untracked-files=all
git diff
git diff --cached

If git status reports a merge or rebase in progress, stop here. Record the state and preserve any files you need before deciding how to finish or abort it. Don’t start a cherry-pick, reset, clean or stash as part of this checklist.

Compare the top-level path and branch with the intended pair. A blank branch name means Git has a detached HEAD, not that it found the right branch. If you use git status --short, ?? means an untracked file: neither diff command includes its contents. If the agent says it made a commit, inspect git log -5 --oneline --decorate and verify the actual commit and its contents, not just its subject. Check the remote branch or pull request directly if a push was possible; your local remote-tracking ref may be stale.

Choose a recovery path based on what actually changed

  • Uncommitted edits: Save the reviewed staged and unstaged diffs separately using git diff --cached --binary and git diff --binary and copy any needed untracked files to a safe location outside the checkout. Keep the source intact while you move only the intended hunks to a clean destination worktree. If your changes and the agent’s edits touch the same lines, resolve them manually. Review the destination diff and run the relevant tests before cleaning up the source.
  • Local commit only: Inspect the commit with git show <sha>. Once you know exactly what it contains, you can cherry-pick it from the correct clean branch. If it includes unrelated changes, transfer only the intended changes instead. Verify the destination before deciding what to do with the original commit.
  • Pushed commit or opened pull request: Check the actual remote branch. For a PR, confirm its source repository and branch, its base, and whether it has already been merged. If others might use that history, agree on the correction with the repository owner. Retargeting a PR doesn’t remove a commit from the branch it was pushed to, and a revert can itself trigger CI or a deployment. Don’t force-push as a first response.

Put a checkpoint before the first edit

Give the agent a path, a branch and a starting commit, not just a branch name. Ask it to report all three from Git and stop if any differs. Make the first change small and confirm that only the intended worktree changed. Keep branch switches, rebases, commits, pushes and PRs out of the task unless you explicitly need them. Before a PR, review the diff and its base again; both can differ from what you checked at the start. The git worktree manual explains which state is shared and which is per checkout.

Put the same checkpoint into an APX project

In APX Projects, register the intended worktree folder as a project if it already contains AGENTS.md or .apx/. In that project’s task, put the expected path, branch and starting commit in the completion criteria. Start with a read-only Git check; then ask for the list of edited files and the final diff from that same folder. Use the task verification guide to specify what evidence you’ll review.

A project gives the agent a folder and instructions, not a Git branch lock. APX documents read-only folders for separate reference material, but its folder checks are not an operating-system sandbox. Keep the Git checks and use your host’s branch protection for shared branches. A successful local check is not permission to publish changes.

Manuel Bruña

About the author

Manuel Bruña

Manuel Bruña (tecnomanu or Manu) is an AI engineer and software developer who creates and develops APX. His work spans AI-powered software, web and mobile applications, cloud development, and leading technical projects. He writes about practical ways to build and work with AI agents.

Sources

  1. Claude Code issue #95968: report of changes in the wrong checkout ↗
  2. Git manual: linked worktrees and shared repository state ↗
  3. APX project folders and read-only references ↗
← All articles