Skip to content

fix: offer stash/migrate options when creating a branch with dirty worktree#304986

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/branch-from-stash-migrate
Open

fix: offer stash/migrate options when creating a branch with dirty worktree#304986
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/branch-from-stash-migrate

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

When using "Git: Create Branch From..." with uncommitted changes, if the target ref differs from the current HEAD, git fails with a DirtyWorkTree error that is not handled gracefully. The user sees a generic error without any option to stash, migrate, or discard their changes.

This is inconsistent with the regular checkout behavior, which presents a dialog with "Stash & Checkout", "Migrate Changes", and "Force Checkout" options.

Closes #191900

What is the new behavior?

The _branch() method now catches DirtyWorkTree errors and presents the same dialog as the checkout command:

  • Stash & Checkout: Stashes changes, creates the branch, and keeps the stash
  • Migrate Changes: Stashes changes, creates the branch, then pops the stash to carry changes forward
  • Force Checkout: Discards all local changes and creates the branch

This mirrors the existing error handling in _checkout().

Additional context

The fix follows the exact same pattern used in _checkout() (around line 2960 of commands.ts) for handling DirtyWorkTree errors, ensuring consistent behavior across all branch switching operations.

…rktree

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 microsoft#191900
@vs-code-engineering vs-code-engineering bot added this to the 1.114.0 milestone Mar 26, 2026
@vs-code-engineering
Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@lszomoru

Matched files:

  • extensions/git/src/commands.ts

@alexr00 alexr00 removed this from the 1.114.0 milestone Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Git - "Create a branch from..." does not offer migrate

3 participants