28

I'm using JGit to checkout a remote tracking branch.

Git binrepository = cloneCmd.call()

CheckoutCommand checkoutCmd = binrepository.checkout();
checkoutCmd.setName( "origin/" + branchName);
checkoutCmd.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK );
checkoutCmd.setStartPoint( "origin/" + branchName );

Ref ref = checkoutCmd.call();

The files are checked out, but the HEAD is not pointing to the branch. Following is the git status output,

$ git status
# Not currently on any branch.
nothing to commit (working directory clean)

The same operation can be performed in git command line, easily and it works,

git checkout -t origin/mybranch

How to do this JGit?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Nambi
  • 2,688
  • 8
  • 28
  • 37

4 Answers4

45

You have to use setCreateBranch to create a branch:

Ref ref = git.checkout().
        setCreateBranch(true).
        setName("branchName").
        setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
        setStartPoint("origin/" + branchName).
        call();

Your first command was the equivalent of git checkout origin/mybranch.

(Edit: I submitted a patch to JGit to improve the documentation of CheckoutCommand: https://git.eclipse.org/r/8259)

robinst
  • 30,027
  • 10
  • 102
  • 108
  • I tried it. it works. its a simple solution. I need to make a change, commit and push to remote. I'll test that and update the thread. – Nambi Oct 17 '12 at 22:26
  • Much complete answer than mine. +1 – VonC Oct 18 '12 at 05:26
  • @AdamSiemion: What do you mean by that? `setStartPoint` should work with a tag name, otherwise it's a bug in JGit (please report it). – robinst Sep 24 '14 at 01:44
  • @robinst To make it work with a git tag name, `origin/` has to be removed, otherwise [this](https://issues.jboss.org/browse/FORGE-2033) will happen. – Adam Siemion Sep 24 '14 at 06:59
  • @AdamSiemion: Yeah, but that's the same as on the command line, tags are not namespaced like branches. – robinst Sep 24 '14 at 13:18
  • @robinst on the cmd line `git checkout ` work: `git checkout 2.1.2.Final` or `git checkout master`. – Adam Siemion Sep 24 '14 at 13:29
  • CreateBranchCommand is giving me an error: `CreateBranchCommand cannot be resolved to a variable`. Any ideas how to fix? – Jsleshem Aug 23 '19 at 13:25
  • is setCreateBranch always be true ? ref: https://stackoverflow.com/questions/45587631/how-to-checkout-a-remote-branch-without-knowing-if-it-exists-locally-in-jgit – Mr.Raindrop Dec 01 '21 at 09:24
5

For whatever reason, the code that robinst posted did not work for me. In particular, the local branch that was created did not track the remote branch. This is what I used that worked for me (using jgit 2.0.0.201206130900-r):

git.pull().setCredentialsProvider(user).call();
git.branchCreate().setForce(true).setName(branch).setStartPoint("origin/" + branch).call();
git.checkout().setName(branch).call();
Jason Wheeler
  • 872
  • 1
  • 9
  • 23
4

As shown in the code of CheckoutCommand, you need to set the boolean createBranch to true in order to create a local branch.

You can see an example in CheckoutCommandTest - testCreateBranchOnCheckout()

@Test
public void testCreateBranchOnCheckout() throws Exception {
  git.checkout().setCreateBranch(true).setName("test2").call();
  assertNotNull(db.getRef("test2"));
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

you also can just like this

git.checkout().setName(remoteBranch).setForce(true).call();
                logger.info("Checkout to remote branch:" + remoteBranch);
                git.branchCreate() 
                   .setName(branchName)
                   .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
                   .setStartPoint(remoteBranch)
                   .setForce(true)
                   .call(); 
                logger.info("create new locale branch:" + branchName + "set_upstream with:" + remoteBranch);
                git.checkout().setName(branchName).setForce(true).call();
                logger.info("Checkout to locale branch:" + branchName);
徐诗云
  • 11
  • 1