From 7391f05cf2d2a7773cbc55e9e9b407d7a757bb88 Mon Sep 17 00:00:00 2001 From: yogeshwaran-c Date: Thu, 26 Mar 2026 06:40:55 +0530 Subject: [PATCH] fix: offer stash/migrate options when creating a branch with dirty worktree When creating a branch from a specific ref with uncommitted changes, git checkout -b fails with DirtyWorkTree but the error is not handled gracefully. Unlike the regular checkout command, the branch creation command does not offer the user the option to stash, migrate, or force discard their changes. Add error handling for DirtyWorkTree in _branch() that mirrors the existing _checkout() behavior, presenting the user with Stash & Checkout, Migrate Changes, and Force Checkout options. Closes #191900 --- extensions/git/src/commands.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 51cbef08d2e68..3700b1be604e5 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -3112,7 +3112,31 @@ export class CommandCenter { return; } - await repository.branch(branchName, true, target); + try { + await repository.branch(branchName, true, target); + } catch (err) { + if (err.gitErrorCode !== GitErrorCodes.DirtyWorkTree) { + throw err; + } + + const stash = l10n.t('Stash & Checkout'); + const migrate = l10n.t('Migrate Changes'); + const force = l10n.t('Force Checkout'); + const choice = await window.showWarningMessage(l10n.t('Your local changes would be overwritten by checkout.'), { modal: true }, stash, migrate, force); + + if (choice === force) { + await this.cleanAll(repository); + await repository.branch(branchName, true, target); + } else if (choice === stash || choice === migrate) { + if (await this._stash(repository, true)) { + await repository.branch(branchName, true, target); + + if (choice === migrate) { + await this.stashPopLatest(repository); + } + } + } + } } private async pickRef(items: Promise, placeHolder: string): Promise {