Source:
<code>public Optional execute() throws InvalidRemoteException { try { final List<Ref> branches = git._branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); final Set<String> remoteBranches = new HashSet<>(); final Set<String> localBranches = new HashSet<>(); for (final Ref branch : branches) { final String branchName = branch.getName().substring(branch.getName().lastIndexOf("/") + 1); if (branch.getName().startsWith("refs/remotes/" + remote.getK1())) { remoteBranches.add(branchName); } else { localBranches.add(branchName); } } for (final String localBranch : localBranches) { if (localBranch.equals("HEAD")) { continue; } if (remoteBranches.contains(localBranch)) { try { git._branchCreate() .setName(localBranch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM) .setStartPoint(remote.getK1() + "/" + localBranch) .setForce(true) .call(); } catch (Throwable t) { throw new RuntimeException("Error creating branch [" + localBranch + "]."); } } } remoteBranches.removeAll(localBranches); for (final String branch : remoteBranches) { <START> if (branch.equals("HEAD")) { <END> continue; } try { git._branchCreate() .setName(branch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM) .setStartPoint(remote.getK1() + "/" + branch) .setForce(true) .call(); } catch (Throwable t) { throw new RuntimeException("Error creating branch [" + branch + "]."); } } return null; } catch (final InvalidRemoteException e) { throw e; } catch (final RuntimeException re) { throw re; } catch (final Exception ex) { throw new RuntimeException(ex); } }</code><technical_language>I read thread, concern removing HEAD branch is potentially lose info if HEAD points another branch master. If UI shows HEAD branch I is another issue</technical_language>

Target:
public Optional execute() throws InvalidRemoteException { try { final List<Ref> branches = git._branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); final Set<String> remoteBranches = new HashSet<>(); final Set<String> localBranches = new HashSet<>(); for (final Ref branch : branches) { final String branchName = branch.getName().substring(branch.getName().lastIndexOf("/") + 1); if (branch.getName().startsWith("refs/remotes/" + remote.getK1())) { remoteBranches.add(branchName); } else { localBranches.add(branchName); } } for (final String localBranch : localBranches) { if (localBranch.equals(Constants.HEAD)) { continue; } if (remoteBranches.contains(localBranch)) { try { git._branchCreate() .setName(localBranch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM) .setStartPoint(remote.getK1() + "/" + localBranch) .setForce(true) .call(); } catch (Throwable t) { throw new RuntimeException("Error creating branch [" + localBranch + "]."); } } } remoteBranches.removeAll(localBranches); for (final String branch : remoteBranches) { if (branch.equals(Constants.HEAD)) { continue; } try { git._branchCreate() .setName(branch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM) .setStartPoint(remote.getK1() + "/" + branch) .setForce(true) .call(); } catch (Throwable t) { throw new RuntimeException("Error creating branch [" + branch + "]."); } } return null; } catch (final InvalidRemoteException e) { throw e; } catch (final RuntimeException re) { throw re; } catch (final Exception ex) { throw new RuntimeException(ex); } }