Community
Participate
Working Groups
Here's a code snippet, which uses JGit's ListBranchCommand to list existing remote branches and creates them locally. Although the tracking option is set prior the call to branchCreateCmd.call(), no tracking configuration is written to .git/config. try { // get a list of branches ListBranchCommand branchList = git.branchList(); branchList.setListMode(ListMode.REMOTE); // want all remote branches List<Ref> remoteRefs = branchList.call(); for (Ref remoteRef: remoteRefs) { String name = remoteRef.getName(); int remoteIndex = (Constants.R_REMOTES + "origin/").length(); //$NON-NLS-1$ // Remove "refs/remotes/origin/" part in branch name name = name.substring(remoteIndex); if (!name.equals(Constants.MASTER)) { CreateBranchCommand branchCreateCmd = git.branchCreate(); branchCreateCmd.setName(name); RevWalk walk = new RevWalk(git.getRepository()); RevCommit startCommit = walk.parseCommit(remoteRef .getObjectId()); branchCreateCmd.setStartPoint(startCommit); // Add remote tracking config branchCreateCmd.setUpstreamMode(SetupUpstreamMode.TRACK); branchCreateCmd.call(); } } } catch (IOException ioException) { ioException.printStackTrace(); } catch (JGitInternalException e) { e.printStackTrace(); } catch (RefAlreadyExistsException e) { e.printStackTrace(); } catch (RefNotFoundException e) { e.printStackTrace(); } catch (InvalidRefNameException e) { e.printStackTrace(); } Replacing RevWalk walk = new RevWalk(git.getRepository()); RevCommit startCommit = walk.parseCommit(remoteRef .getObjectId()); branchCreateCmd.setStartPoint(startCommit); with branchCreateCmd.setStartPoint(remoteRef.getName()); works.