@@ -1298,8 +1298,6 @@ export class LocalGitProvider implements GitProvider, Disposable {
1298
1298
args . push ( '--force' ) ;
1299
1299
}
1300
1300
1301
- await this . git . branch ( repoPath , ...args , ...branches . map ( ( b : GitBranchReference ) => b . ref ) ) ;
1302
-
1303
1301
if ( options . remote ) {
1304
1302
const trackingBranches = localBranches . filter ( b => b . upstream != null ) ;
1305
1303
if ( trackingBranches . length !== 0 ) {
@@ -1308,17 +1306,43 @@ export class LocalGitProvider implements GitProvider, Disposable {
1308
1306
) ;
1309
1307
1310
1308
for ( const [ remote , branches ] of branchesByOrigin . entries ( ) ) {
1311
- await this . git . push ( repoPath , {
1312
- delete : {
1313
- remote : remote ,
1314
- branches : branches . map ( b => getBranchNameWithoutRemote ( b . upstream ! . name ) ) ,
1315
- } ,
1309
+ const remoteCommitByBranch : Map < string , string > = { } ;
1310
+ branches . forEach ( async b => {
1311
+ remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ b . ref } ` , {
1312
+ maxResults : 1 ,
1313
+ } ) ;
1314
+ remoteCommitByBranch [ b . ref ] = remoteCommit ;
1316
1315
} ) ;
1316
+
1317
+ await this . git . branch (
1318
+ repoPath ,
1319
+ '--delete' ,
1320
+ '--remotes' ,
1321
+ ...branches . map ( ( b : GitBranchReference ) => `${ remote } /${ b . ref } ` ) ,
1322
+ ) ;
1323
+
1324
+ try {
1325
+ await this . git . branch ( repoPath , ...args , ...branches . map ( ( b : GitBranchReference ) => b . ref ) ) ;
1326
+ await this . git . push ( repoPath , {
1327
+ delete : {
1328
+ remote : remote ,
1329
+ branches : branches . map ( b => getBranchNameWithoutRemote ( b . upstream ! . name ) ) ,
1330
+ } ,
1331
+ } ) ;
1332
+ } catch ( ex ) {
1333
+ // If it fails, restore the remote branches
1334
+ remoteCommitByBranch . forEach ( async ( branch , commit ) => {
1335
+ await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch } ` , commit ) ;
1336
+ await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1337
+ } ) ;
1338
+ throw ex ;
1339
+ }
1317
1340
}
1318
1341
}
1319
1342
}
1320
1343
}
1321
1344
1345
+ const remoteBranches = branches . filter ( ( b : GitBranchReference ) => b . remote ) ;
1322
1346
if ( remoteBranches . length !== 0 ) {
1323
1347
const branchesByOrigin = groupByMap ( remoteBranches , b => getRemoteNameFromBranchName ( b . name ) ) ;
1324
1348
0 commit comments