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