@@ -91,20 +91,38 @@ export class PullRequestGitHelper {
9191 const remoteName = remote . remoteName ;
9292 let branch : Branch ;
9393
94+ // Always fetch the remote branch first to ensure we have the latest commits
95+ const trackedBranchName = `refs/remotes/${ remoteName } /${ branchName } ` ;
96+ Logger . appendLine ( `Fetch tracked branch ${ trackedBranchName } ` , PullRequestGitHelper . ID ) ;
97+ progress . report ( { message : vscode . l10n . t ( 'Fetching branch {0}' , branchName ) } ) ;
98+ await repository . fetch ( remoteName , branchName ) ;
99+ const trackedBranch = await repository . getBranch ( trackedBranchName ) ;
100+
94101 try {
95102 branch = await repository . getBranch ( branchName ) ;
96103 // Make sure we aren't already on this branch
97104 if ( repository . state . HEAD ?. name === branch . name ) {
98105 Logger . appendLine ( `Tried to checkout ${ branchName } , but branch is already checked out.` , PullRequestGitHelper . ID ) ;
99106 return ;
100107 }
108+
109+ // Check if local branch is pointing to the same commit as the remote
110+ if ( branch . commit !== trackedBranch . commit ) {
111+ Logger . debug ( `Local branch ${ branchName } commit ${ branch . commit } differs from remote commit ${ trackedBranch . commit } . Updating local branch.` , PullRequestGitHelper . ID ) ;
112+ progress . report ( { message : vscode . l10n . t ( 'Updating branch {0} to match remote' , branchName ) } ) ;
113+ // Delete and recreate the local branch to point to the remote commit
114+ await repository . deleteBranch ( branchName , true ) ;
115+ await repository . createBranch ( branchName , false , trackedBranch . commit ) ;
116+ // Get the updated branch reference
117+ branch = await repository . getBranch ( branchName ) ;
118+ }
119+
101120 Logger . debug ( `Checkout ${ branchName } ` , PullRequestGitHelper . ID ) ;
102121 progress . report ( { message : vscode . l10n . t ( 'Checking out {0}' , branchName ) } ) ;
103122 await repository . checkout ( branchName ) ;
104123
105124 if ( ! branch . upstream ) {
106125 // this branch is not associated with upstream yet
107- const trackedBranchName = `refs/remotes/${ remoteName } /${ branchName } ` ;
108126 await repository . setBranchUpstream ( branchName , trackedBranchName ) ;
109127 }
110128
@@ -114,16 +132,11 @@ export class PullRequestGitHelper {
114132 await repository . pull ( ) ;
115133 }
116134 } catch ( err ) {
117- // there is no local branch with the same name, so we are good to fetch, create and checkout the remote branch.
135+ // there is no local branch with the same name, so we are good to create and checkout the remote branch.
118136 Logger . appendLine (
119- `Branch ${ remoteName } / ${ branchName } doesn't exist on local disk yet.` ,
137+ `Branch ${ branchName } doesn't exist on local disk yet. Creating from remote .` ,
120138 PullRequestGitHelper . ID ,
121139 ) ;
122- const trackedBranchName = `refs/remotes/${ remoteName } /${ branchName } ` ;
123- Logger . appendLine ( `Fetch tracked branch ${ trackedBranchName } ` , PullRequestGitHelper . ID ) ;
124- progress . report ( { message : vscode . l10n . t ( 'Fetching branch {0}' , branchName ) } ) ;
125- await repository . fetch ( remoteName , branchName ) ;
126- const trackedBranch = await repository . getBranch ( trackedBranchName ) ;
127140 // create branch
128141 progress . report ( { message : vscode . l10n . t ( 'Creating and checking out branch {0}' , branchName ) } ) ;
129142 await repository . createBranch ( branchName , true , trackedBranch . commit ) ;
0 commit comments