java JGit 分支结帐问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12614631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:33:35  来源:igfitidea点击:

JGit branch checkout Issue

javaapigithubjgit

提问by Muthu

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

如果我结帐分行

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: Ref kalees can not beresolution。

What is the issue here, if i specify "master"instead of "kalees", it works fine. what change should i do to checkout a specific branch?

这里有什么问题,如果我指定“master”而不是“kalees”它工作正常。我应该做哪些更改来结帐特定分支?

if i use the code

如果我使用代码

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

It checkout the kaleesbranch. but when i do pull operation

它结帐羽衣甘蓝分支。但是当我做拉操作时

git.pull().call(); 

it throws org.eclipse.jgit.api.errors.DetachedHeadException: HEAD is detached. What could be the , whether this is a checkout issue or pull issue ?

它抛出org.eclipse.jgit.api.errors.DetachedHeadException: HEAD is detached。可能是什么,这是结帐问题还是拉取问题?

回答by VonC

It should only happen if:

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

  • kaleesisn't an existing branch (or is incorrectly written, bad case)
  • kaleesis a remote branch you haven tracked yet a a local branch
  • kalees不是现有的分支(或者写错了,不好的情况)
  • 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(); 

回答by Linsama

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 :)

当我向存储库提交一些内容时,它就解决了。希望对你有帮助:)

回答by takumi3t9

Muthu your code is working you only need to add origin/branch like this to the branch call

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

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