0

Trying to checkout remote branches for repository:

Command: git.checkout().setCreateBranch(true).setName(<branchName>).setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call()

However, it is setting the new branch HEAD at changes from master branch in local.

The git command I need to recreate git checkout -b <branchName> - Where head of new branch should be same as head of remote

torek
  • 448,244
  • 59
  • 642
  • 775
  • 2
    Does this answer your question? [JGit: Checkout a remote branch](https://stackoverflow.com/questions/12927163/jgit-checkout-a-remote-branch) – Rüdiger Herrmann Jul 30 '21 at 08:06
  • No, I had to change my code to download all branches when I clone my repo. It seems if the local doesn't have reference of remote branch, it is not able to checkout with the command in the given answer – incareofvarx Jul 30 '21 at 15:08
  • 1
    In order to check out a remote branch it needs to be present in the local repository. Note, that this is a concept of Git and not specific to JGit. To make remote branches available locally you need to fetch (clone = init + fetch) them first. – Rüdiger Herrmann Jul 30 '21 at 16:31
  • How to fetch remote branches in my local repo from jgit, I am using ```git.fetch() .setRemote(remote.getName()) .setRefSpecs(remote.getFetchRefSpecs()) .call();``` but it isnt able to save – incareofvarx Jul 30 '21 at 22:30
  • Without further details it is hard to say why 'it isn't able to save'. As this question seems answered by https://stackoverflow.com/questions/12927163/jgit-checkout-a-remote-branch, I recommend to close this and create a new question, including an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example). – Rüdiger Herrmann Jul 31 '21 at 10:23
  • I got the answer when the repo is cloned, it's only checking out a particular branch, and thus it isn't able to get references of other branches existing only in the remote when it has to switch. Thank you – incareofvarx Aug 04 '21 at 17:50
  • See here how to fetch all branches while cloning: https://stackoverflow.com/questions/65612910/jgit-not-cloning-all-branches or specify a list of known branches to clone with `CloneCommand::setBranchesToClone`. Note that branches need to be specified with their full ref, e.g. `refs/heads/foo`. – Rüdiger Herrmann Aug 04 '21 at 19:56

0 Answers0