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