@@ -1263,8 +1263,6 @@ export class LocalGitProvider implements GitProvider, Disposable {
1263
1263
args . push ( '--force' ) ;
1264
1264
}
1265
1265
1266
- await this . git . branch ( repoPath , ...args , ...branches . map ( ( b : GitBranchReference ) => b . ref ) ) ;
1267
-
1268
1266
if ( options . remote ) {
1269
1267
const trackingBranches = localBranches . filter ( b => b . upstream != null ) ;
1270
1268
if ( trackingBranches . length !== 0 ) {
@@ -1273,17 +1271,43 @@ export class LocalGitProvider implements GitProvider, Disposable {
1273
1271
) ;
1274
1272
1275
1273
for ( const [ remote , branches ] of branchesByOrigin . entries ( ) ) {
1276
- await this . git . push ( repoPath , {
1277
- delete : {
1278
- remote : remote ,
1279
- branches : branches . map ( b => getBranchNameWithoutRemote ( b . upstream ! . name ) ) ,
1280
- } ,
1274
+ const remoteCommitByBranch : Map < string , string > = { } ;
1275
+ branches . forEach ( async b => {
1276
+ remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ b . ref } ` , {
1277
+ maxResults : 1 ,
1278
+ } ) ;
1279
+ remoteCommitByBranch [ b . ref ] = remoteCommit ;
1281
1280
} ) ;
1281
+
1282
+ await this . git . branch (
1283
+ repoPath ,
1284
+ '--delete' ,
1285
+ '--remotes' ,
1286
+ ...branches . map ( ( b : GitBranchReference ) => `${ remote } /${ b . ref } ` ) ,
1287
+ ) ;
1288
+
1289
+ try {
1290
+ await this . git . branch ( repoPath , ...args , ...branches . map ( ( b : GitBranchReference ) => b . ref ) ) ;
1291
+ await this . git . push ( repoPath , {
1292
+ delete : {
1293
+ remote : remote ,
1294
+ branches : branches . map ( b => getBranchNameWithoutRemote ( b . upstream ! . name ) ) ,
1295
+ } ,
1296
+ } ) ;
1297
+ } catch ( ex ) {
1298
+ // If it fails, restore the remote branches
1299
+ remoteCommitByBranch . forEach ( async ( branch , commit ) => {
1300
+ await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch } ` , commit ) ;
1301
+ await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1302
+ } ) ;
1303
+ throw ex ;
1304
+ }
1282
1305
}
1283
1306
}
1284
1307
}
1285
1308
}
1286
1309
1310
+ const remoteBranches = branches . filter ( ( b : GitBranchReference ) => b . remote ) ;
1287
1311
if ( remoteBranches . length !== 0 ) {
1288
1312
const branchesByOrigin = groupByMap ( remoteBranches , b => getRemoteNameFromBranchName ( b . name ) ) ;
1289
1313
0 commit comments