fix(worktree): fetch base branch before creating worktree#35
Merged
Conversation
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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
origin/<baseBranch>, a local remote-tracking ref that only reflects the lastgit 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.createWorktreeForChatnow best-effort-fetchesorigin <baseBranch>before callinggit worktree add, using the existingcreateGitForNetwork(2-min timeout) wrapped inwithGitLockfor consistency with other fetch call sites.console.warn— worktree creation proceeds unchanged, so offline/auth/missing-remote scenarios behave exactly as before.branchType === "local"(user opted into a local ref) and when the repo has nooriginremote.Test plan
origin/mainfrom another clone, then create a chat withuseWorktree: true, basemain. Verify the worktree'sHEADmatches the just-pushed remote commit (git -C ~/.21st/worktrees/<slug>/<folder> rev-parse HEAD).originat 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 cachedorigin/<baseBranch>, no user-facing error.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.withGitLockshould serialize the two fetches cleanly.