@@ -1287,8 +1287,6 @@ export class LocalGitProvider implements GitProvider, Disposable {
12871287 args . push ( '--force' ) ;
12881288 }
12891289
1290- await this . git . branch ( repoPath , ...args , ...branches . map ( ( b : GitBranchReference ) => b . ref ) ) ;
1291-
12921290 if ( options . remote ) {
12931291 const trackingBranches = localBranches . filter ( b => b . upstream != null ) ;
12941292 if ( trackingBranches . length !== 0 ) {
@@ -1297,17 +1295,43 @@ export class LocalGitProvider implements GitProvider, Disposable {
12971295 ) ;
12981296
12991297 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 ;
13051304 } ) ;
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+ }
13061329 }
13071330 }
13081331 }
13091332 }
13101333
1334+ const remoteBranches = branches . filter ( ( b : GitBranchReference ) => b . remote ) ;
13111335 if ( remoteBranches . length !== 0 ) {
13121336 const branchesByOrigin = groupByMap ( remoteBranches , b => getRemoteNameFromBranchName ( b . name ) ) ;
13131337
0 commit comments