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