EnterWorktree branch rename preserves upstream tracking to base branch, causing accidental pushes

Resolved 💬 3 comments Opened Mar 25, 2026 by frizar Closed Mar 25, 2026

Summary

When EnterWorktree (or Agent with isolation: "worktree") creates a worktree, the temporary branch (worktree-agent-<uuid>) is created from HEAD and inherits upstream tracking to the base branch (e.g., origin/develop). When the branch is later renamed to a feature branch name, git branch -m preserves the upstream tracking config. A subsequent git push then pushes directly to the base branch instead of the feature branch.

Reproduction

  1. Main worktree is on develop
  2. Use EnterWorktree or Agent with isolation: "worktree" — creates worktree-agent-abc123 from HEAD
  3. Branch gets renamed to feature/my-feature
  4. Commit changes
  5. Run git push

Expected: Push fails or targets origin/feature/my-feature
Actual: Push goes to origin/develop (the tracked upstream from step 2)

Evidence

# Reflog showing the rename:
e582a333c  Branch: renamed refs/heads/worktree-agent-aaf16e68 to refs/heads/feature/SD-136721

# Git config after rename still tracks develop:
branch.feature/SD-136721.remote=origin
branch.feature/SD-136721.merge=refs/heads/develop

# Leftover worktree branches also track develop:
branch.worktree-agent-ae48e0c1.merge=refs/heads/develop

Root Cause

git branch -m old new renames the config section but preserves all values, including .merge (upstream tracking). Claude Code does not call git branch --unset-upstream or git branch --set-upstream-to after the rename.

Suggested Fix

After renaming the worktree branch to the target name, run:

git branch --unset-upstream <new-branch-name>

This ensures the renamed branch has no upstream, so:

  • push.default=simple will refuse to push (no upstream = no default target)
  • The user must explicitly git push -u origin <branch> on first push, which sets correct tracking

Workaround

git config --global push.default current

This makes git push always target origin/<local-branch-name> regardless of tracking config.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗