Skip to content

Commit db793c6

Browse files
Copilotalexr00
andauthored
fix: handle existing local branch when creating worktree
Agent-Logs-Url: https://github.com/microsoft/vscode-pull-request-github/sessions/c7f23b9d-14de-42d2-960a-36aa53e2d704 Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 8ea92b4 commit db793c6

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/commands.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -909,12 +909,30 @@ export function registerCommands(
909909
// Continue even if fetch fails - the branch might already be available locally
910910
}
911911

912+
// Check if the branch already exists locally
913+
let branchExists = false;
914+
try {
915+
await repositoryToUse.getBranch(branchName);
916+
branchExists = true;
917+
} catch {
918+
// Branch doesn't exist locally, we'll create it
919+
branchExists = false;
920+
}
921+
912922
// Use the git extension's createWorktree API
913-
await repositoryToUse.createWorktree!({
914-
path: worktreePath,
915-
commitish: trackedBranchName,
916-
branch: branchName
917-
});
923+
// If branch already exists, don't specify the branch parameter to avoid "branch already exists" error
924+
if (branchExists) {
925+
await repositoryToUse.createWorktree!({
926+
path: worktreePath,
927+
commitish: branchName
928+
});
929+
} else {
930+
await repositoryToUse.createWorktree!({
931+
path: worktreePath,
932+
commitish: trackedBranchName,
933+
branch: branchName
934+
});
935+
}
918936
}
919937
);
920938

0 commit comments

Comments
 (0)