Issuing a JGit branch branch

I am checking the repository from github using the following code.

private String url = "https://github.com/organization/project.git"; Git repo = Git.cloneRepository().setURI(url).setDirectory(directory).setCloneAllBranches(true).call(); for (Ref b : repo.branchList().call()) { System.out.println("(standard): cloned branch " + b.getName()); } 

I am using code

 Git git = Git.open(checkout); //checkout is the folder with .git git.pull().call(); //succeeds 

If I chekout a branch

 Git git = Git.open(new File(checkout)); //checkout is the folder with .git System.out.println(git.getRepository().getFullBranch()); CheckoutCommand checkout = git.checkout(); Ref call = checkout.setName("kalees").call(); 

It throws org.eclipse.jgit.api.errors.RefNotFoundException: Ref kalees cannot be resolved.

What is the problem, if I specify "master" instead of "feces" , it works fine . What change should I make to check for a specific branch?

if i use code

 git.checkout().setCreateBranch(true).setName("refs/remotes/origin/kalees"); 

He checks the branch of feces . but when i do the operation

 git.pull().call(); 

it throws org.eclipse.jgit.api.errors.DetachedHeadException: HEAD is disconnected . What could be, whether it is a problem with an extract or a problem with a problem?

+4
source share
3 answers

This should only happen if:

  • kalees not an existing branch (or misspelled, bad case)
  • kalees is the remote branch that you already tracked, but the local branch

If so, you may need to create one first (a bit like this example )

 git.branchCreate().setForce(true).setName("kalees").setStartPoint("origin/kalees").call(); 

Following β€œ JGit: I can’t find a tutorial or a simple example ”, I would prefer to use:

 git.branchCreate() .setName("kalees") .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM) .setStartPoint("origin/kalees") .setForce(true) .call(); 
+3
source

I met this question when I want to create a branch with an empty repository, there is no commit in this repository.

He allowed when I transferred something to the repository. Hope this will be helpful for you :)

+1
source

Muthu your code works, you only need to add a start / branch like this to the branch call

 Ref call = checkout.setName("origin/kalees").call(); 
0
source

Source: https://habr.com/ru/post/1436514/


All Articles