Skip to content

fix(worktree): fetch base branch before creating worktree#35

Merged
aletc1 merged 1 commit intodevfrom
claude/beautiful-brown-754079
Apr 24, 2026
Merged

fix(worktree): fetch base branch before creating worktree#35
aletc1 merged 1 commit intodevfrom
claude/beautiful-brown-754079

Conversation

@aletc1
Copy link
Copy Markdown
Owner

@aletc1 aletc1 commented Apr 24, 2026

Summary

  • Worktrees were being created off origin/<baseBranch>, a local remote-tracking ref that only reflects the last git fetch. When the remote had advanced, the new worktree was born stale and caused avoidable merge conflicts later when users tried to merge or open a PR.
  • createWorktreeForChat now best-effort-fetches origin <baseBranch> before calling git worktree add, using the existing createGitForNetwork (2-min timeout) wrapped in withGitLock for consistency with other fetch call sites.
  • Fetch failures are caught and logged via console.warn — worktree creation proceeds unchanged, so offline/auth/missing-remote scenarios behave exactly as before.
  • Skipped when branchType === "local" (user opted into a local ref) and when the repo has no origin remote.

Test plan

  • Happy path — remote ahead of local: push a commit to origin/main from another clone, then create a chat with useWorktree: true, base main. Verify the worktree's HEAD matches the just-pushed remote commit (git -C ~/.21st/worktrees/<slug>/<folder> rev-parse HEAD).
  • Offline / fetch failure: disable the network (or point origin at a bad URL) and create a chat. Expect [worktree] Pre-create fetch of origin/<baseBranch> failed: … in the main-process console, worktree still created against cached origin/<baseBranch>, no user-facing error.
  • No origin remote: in a repo without origin, create a chat. Expect no fetch attempt, no warning, worktree created off local branch as before.
  • branchType === "local": pick an explicit local branch when creating a chat. Expect no fetch attempt; worktree branches from the chosen local ref unchanged.
  • Concurrency: create two chats back-to-back against the same project — lock via withGitLock should serialize the two fetches cleanly.

Worktrees were being created off `origin/<baseBranch>`, a local remote-tracking ref that only reflects the last fetch. When the remote had advanced, the new worktree was born stale and caused avoidable merge conflicts later. Now `createWorktreeForChat` best-effort-fetches `origin <baseBranch>` before calling `git worktree add`; fetch failures are logged and never block creation. Skipped for `branchType === "local"` and repos without an origin remote.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@aletc1 aletc1 merged commit 278f040 into dev Apr 24, 2026
@aletc1 aletc1 deleted the claude/beautiful-brown-754079 branch April 24, 2026 08:19
aletc1 added a commit that referenced this pull request Apr 24, 2026
Follow-up to #35. The pre-create fetch was gated on
`branchType !== "local"`, but the new-chat UI auto-selected the local
variant of the default branch whenever both local and remote existed —
so in the common case (local `main` tracking `origin/main`) the fetch
never ran and worktrees inherited whatever stale state the user had
locally.

- Auto-select prefers the remote variant of the default branch, so the
  worktree starts from a fresh `origin/<branch>`.
- `git fetch origin <baseBranch>` now runs whenever origin exists,
  regardless of branch type. Failures remain best-effort.

User's local branch is left untouched. Explicit local branch selection
is still honored.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant