From 8c827e4128669df52efbe48bc80e9ea7dd9442c0 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Fri, 27 Mar 2026 12:49:56 -0700 Subject: [PATCH] fix: avoid --delete-branch to prevent worktree branch deletion error gh pr merge --delete-branch fails when the current worktree is on the branch being merged, because git refuses to delete a checked-out branch. Instead, merge without --delete-branch and delete only the remote branch afterward. Local branch cleanup is left to the worktree manager. --- src/main/github/client.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/github/client.ts b/src/main/github/client.ts index 055db10d1fd..ea5f1bbcfb2 100644 --- a/src/main/github/client.ts +++ b/src/main/github/client.ts @@ -249,11 +249,34 @@ export async function mergePR( ): Promise<{ ok: true } | { ok: false; error: string }> { await acquire() try { - await execFileAsync('gh', ['pr', 'merge', String(prNumber), `--${method}`, '--delete-branch'], { + await execFileAsync('gh', ['pr', 'merge', String(prNumber), `--${method}`], { cwd: repoPath, encoding: 'utf-8', env: { ...process.env, GH_PROMPT_DISABLED: '1' } }) + // Delete remote branch separately — local branch deletion fails when + // the user's worktree is checked out on that branch, so we only clean + // up the remote side. Local cleanup is left to the caller / worktree manager. + const headBranch = ( + await execFileAsync( + 'gh', + ['pr', 'view', String(prNumber), '--json', 'headRefName', '-q', '.headRefName'], + { + cwd: repoPath, + encoding: 'utf-8' + } + ) + ).stdout.trim() + if (headBranch) { + try { + await execFileAsync('git', ['push', 'origin', '--delete', headBranch], { + cwd: repoPath, + encoding: 'utf-8' + }) + } catch { + // Remote branch may already be deleted by GitHub — ignore + } + } return { ok: true } } catch (err) { const message =