Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 24, 2025

✅ UPDATED: Safe approach to avoid deleting user's local branch

Addressed feedback from @alexr00: The original approach of deleting the user's local branch was dangerous and could lose user work.

🔧 New Safe Approach

Instead of deleting the user's local branch when it has a different commit than the PR:

  1. Preserve the user's local branch - never delete it, protecting any work they may have done
  2. Create a unique branch name using the existing calculateUniqueBranchNameForPR function (e.g., pr/author/123)
  3. Point the new branch to the correct remote commit from the PR
  4. Set up proper upstream tracking to the PR's remote branch
  5. Check out the new unique branch so user gets the correct PR code

✅ Changes Made

src/github/pullRequestGitHelper.ts

  • Removed dangerous deleteBranch operation
  • Added safe unique branch creation using existing calculateUniqueBranchNameForPR function
  • Preserved user's original local branch with their work intact
  • Proper variable management to avoid const reassignment issues
  • Clear logging to show what's happening ("Creating branch pr/author/123 for PR checkout")

src/test/github/pullRequestGitHelper.test.ts

  • Updated test expectations to verify original branch is preserved
  • Test unique branch creation with correct commit and upstream tracking
  • Covers both scenarios: not checked out and currently checked out on conflicting branch

🎯 User Experience

  • User's work is protected - their local branch is never touched
  • Clear branch naming - PR branches use the established pr/author/number pattern
  • Proper tracking - new branch correctly tracks the PR's remote branch
  • Intuitive behavior - user gets the PR code without losing their work

This approach is much safer and follows the existing patterns in the codebase for handling PR branches.

Fixes #7702


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 4 commits September 24, 2025 13:42
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Copilot AI changed the title [WIP] Wrong commit is checked out when local branch exists with the same name Fix wrong commit checkout when local branch exists with same name as PR Sep 24, 2025
Copilot AI requested a review from alexr00 September 24, 2025 14:06
Comment on lines 107 to 109
// Delete and recreate the local branch to point to the remote commit
await repository.deleteBranch(branchName, true);
await repository.createBranch(branchName, false, trackedBranch.commit);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot This is dangerous. We shouldn't just delete the user's local branch. Instead, we should create a local branch with a different, available, name, and point the branch's remote to the correct upstream branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right - that was dangerous. I've fixed it to create a unique branch name (using the existing calculateUniqueBranchNameForPR function) instead of deleting the user's branch. The fix in commit d440a0d now:

  1. Preserves the user's local branch with their work intact
  2. Creates a new branch with a unique name (e.g., pr/author/123)
  3. Points the new branch to the correct remote commit
  4. Sets up proper upstream tracking
  5. Checks out the new unique branch

This approach protects the user's work while still ensuring they get the correct PR code.

Copilot AI and others added 2 commits September 25, 2025 10:53
- Addressed feedback from @alexr00 about dangerous branch deletion
- Instead of deleting user's local branch, create unique branch name
- Use existing calculateUniqueBranchNameForPR function for naming
- Preserve user's work by never touching their original branch
- New approach follows existing PR branch naming patterns
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
@alexr00 alexr00 marked this pull request as ready for review September 25, 2025 14:39
@alexr00 alexr00 enabled auto-merge (squash) September 25, 2025 14:39
@vs-code-engineering vs-code-engineering bot added this to the September 2025 milestone Sep 25, 2025
@alexr00 alexr00 merged commit f2fed53 into main Sep 25, 2025
6 checks passed
@alexr00 alexr00 deleted the copilot/fix-c1f22f83-51a0-429b-a887-6e07b1f894fb branch September 25, 2025 14:58
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.

Wrong commit is checked out when local branch exists with the same name

3 participants