jGit CheckoutCommand results in a strange behaviour

138 Views Asked by At

I'm trying to use CheckoutCommand the following way:

Git git = new Git(repository);
CheckoutCommand checkoutCommand = git.checkout();       
checkoutCommand.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM);
checkoutCommand.setStartPoint("origin/" + branchName);
checkoutCommand.setCreateBranch(true);
checkoutCommand.setForce(true);

checkoutCommand.call();

I tried using SetupUpstreamMode.TRACK as well but it still failed.

This results in a strange behaviour: the repository contents are deleted and instead a clone from each remote branch is created.

Can you please advise?

1

There are 1 best solutions below

0
On

You are not setting the name of the branch to create, which should result in an exception on call() (is it possible that you are swallowing that?). Add a call like this:

 checkoutCommand.setName(branchName);

See the documentation of CheckoutCommand (the above is mentioned here).

Also note that these calls can be chained, so you could also write it like this:

git.checkout().setCreateBranch(true).setName(branchName) // ...