简体   繁体   English

JGit分支结账问题

[英]JGit branch checkout Issue

I am checking out a repository from github using the following code . 我正在使用以下代码从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());
    }

i am using the code 我正在使用该代码

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

If i chekout a branch 如果我chekout一个分支

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 can not be resolved. 它抛出org.eclipse.jgit.api.errors.RefNotFoundException:无法解析引用的kalees。

What is the issue here, if i specify "master" instead of "kalees" , it works fine . 这里有什么问题,如果我指定“master”而不是“kalees”它可以正常工作 what change should i do to checkout a specific branch? 我应该做什么改变来结账一个特定的分支?

if i use the code 如果我使用代码

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

It checkout the kalees branch. 它结帐了kalees分支。 but when i do pull operation 但是当我拉动操作时

git.pull().call(); 

it throws org.eclipse.jgit.api.errors.DetachedHeadException: HEAD is detached . 它抛出org.eclipse.jgit.api.errors.DetachedHeadException:HEAD被分离 What could be the , whether this is a checkout issue or pull issue ? 可能是什么,这是一个结账问题还是拉动问题?

It should only happen if: 它应该只发生在:

  • kalees isn't an existing branch (or is incorrectly written, bad case) kalees不是现有的分支(或kalees了,坏的情况)
  • kalees is a remote branch you haven tracked yet aa local branch kalees是一个远程分支,你已经跟踪了一个本地分支

If so you might need to create it first (a bit like in this example ) 如果是这样,您可能需要先创建它(有点像这个例子

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

Following " JGit: Cannot find a tutorial or simple example ", I would rather use: 在“ JGit:无法找到教程或简单示例 ”之后,我宁愿使用:

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

I met this question when I want to create a branch with an empty repository, there is no commit in this repository. 当我想创建一个具有空存储库的分支时,我遇到了这个问题,此存储库中没有提交。

It's resolved when I commit something to the repository. 当我向存储库提交内容时,它已得到解决。 Hope it's helpful for you :) 希望它对你有帮助:)

Muthu你的代码工作你只需要将这样的origin / branch添加到分支调用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM