diff --git a/apps/desktop/electron/git-worktree-ops.ts b/apps/desktop/electron/git-worktree-ops.ts index dcde29b682c3..e84324a004b5 100644 --- a/apps/desktop/electron/git-worktree-ops.ts +++ b/apps/desktop/electron/git-worktree-ops.ts @@ -251,7 +251,25 @@ async function addWorktree(repoPath, options, gitBin) { const args = ['worktree', 'add', '-b', branch, dir] if (opts.base) { - args.push(String(opts.base)) + // Remote-tracking branches may be stale or missing if the user hasn't + // fetched recently. When the base is an `origin/…` ref, fetch just that + // branch so `git worktree add -b new origin/main` works against the + // latest remote commit. Local branches are used as-is. + const base = String(opts.base) + + if (base.startsWith('origin/')) { + const remoteBranch = base.slice('origin/'.length) + + try { + await runGit(gitBin, ['fetch', 'origin', remoteBranch], root) + } catch { + // The fetch isn't mandatory, but it would be nice to do if possible. + // If it's not possible, just use the local ref of the remote branch. + // If it doesn't exist locally, we'll get an error anyways + } + } + + args.push(base) } try { diff --git a/hermes_cli/web_git.py b/hermes_cli/web_git.py index 6f0dcfd885ab..0af12106745a 100644 --- a/hermes_cli/web_git.py +++ b/hermes_cli/web_git.py @@ -597,7 +597,15 @@ def worktree_add(cwd: str, options: dict) -> dict: target = _unique_dir(os.path.join(root, ".worktrees", slug)) args = ["worktree", "add", "-b", branch, target] if options.get("base"): - args.append(str(options["base"])) + base = str(options["base"]) + # Remote-tracking branches may be stale or missing; fetch just that + # branch so the local ref is up to date before branching. Ignore fetch + # failures (offline / no remote) — git will use whatever local ref + # exists, or raise a clear error below if the ref is entirely missing. + if base.startswith("origin/"): + remote_branch = base[len("origin/"):] + _git(root, ["fetch", "origin", remote_branch]) + args.append(base) code, _, err = _git(root, args) if code != 0: if "already exists" in (err or "").lower():