-
Notifications
You must be signed in to change notification settings - Fork 704
Fix wrong commit checkout when local branch exists with same name as PR #7846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix wrong commit checkout when local branch exists with same name as PR #7846
Conversation
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>
src/github/pullRequestGitHelper.ts
Outdated
| // Delete and recreate the local branch to point to the remote commit | ||
| await repository.deleteBranch(branchName, true); | ||
| await repository.createBranch(branchName, false, trackedBranch.commit); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- Preserves the user's local branch with their work intact
- Creates a new branch with a unique name (e.g.,
pr/author/123) - Points the new branch to the correct remote commit
- Sets up proper upstream tracking
- Checks out the new unique branch
This approach protects the user's work while still ensuring they get the correct PR code.
- 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>
✅ 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:
calculateUniqueBranchNameForPRfunction (e.g.,pr/author/123)✅ Changes Made
src/github/pullRequestGitHelper.tsdeleteBranchoperationcalculateUniqueBranchNameForPRfunctionsrc/test/github/pullRequestGitHelper.test.ts🎯 User Experience
pr/author/numberpatternThis 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.