JGit分支结帐问题

Mut*_*thu 5 java api github jgit

我正在使用以下代码从github中检出一个存储库。

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());
    }
Run Code Online (Sandbox Code Playgroud)

我正在使用代码

Git git = Git.open(checkout); //checkout is the folder with .git
git.pull().call(); //succeeds
Run Code Online (Sandbox Code Playgroud)

如果我去支店

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();
Run Code Online (Sandbox Code Playgroud)

引发org.eclipse.jgit.api.errors.RefNotFoundException:引用kalees无法解析。

这是什么问题,如果我指定“ master”而不是“ kalees”它可以正常工作。结帐特定分支我应该怎么做?

如果我使用代码

git.checkout().setCreateBranch(true).setName("refs/remotes/origin/kalees");
Run Code Online (Sandbox Code Playgroud)

它签出kalees分支。但是当我做拉动操作时

git.pull().call(); 
Run Code Online (Sandbox Code Playgroud)

它抛出org.eclipse.jgit.api.errors.DetachedHeadException:HEAD被分离。这是结帐问题还是出库问题,可能是什么?

Von*_*onC 5

只有在以下情况下才会发生:

  • kalees 不是现有的分支(或者写错了,不好的情况)
  • kalees 是您尚未跟踪的远程分支是本地分支

如果是这样,您可能需要先创建它(有点像在这个例子中

git.branchCreate().setForce(true).setName("kalees").setStartPoint("origin/kalees").call();
Run Code Online (Sandbox Code Playgroud)

在“ JGit:找不到教程或简单示例”之后,我宁愿使用:

git.branchCreate() 
       .setName("kalees")
       .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
       .setStartPoint("origin/kalees")
       .setForce(true)
       .call(); 
Run Code Online (Sandbox Code Playgroud)