我遇到了无法创建本地分支的问题。
Error: org.eclipse.jgit.api.errors.RefNotFoundException: Ref origin/sideMerge cannot be resolved
我已经在Stackoverflow中检查了以下topics和其他一些内容,但它似乎有些可疑或者我还不了解的东西。
有人能指出我对Ref不了解的方向吗? 据我所知,本地refs从origin /“other_branch”开始
代码段:
Git git;
CreateBranchCommand tmpCreateBranch;
git = new Git(existingRepo);
tmpCreateBranch = git.branchCreate();
try {
tmpCreateBranch.setName(tmpBranchName).setStartPoint("origin/" + tmpBranchName).setForce(true).call();
} catch (Exception e) {
System.out.println("Error in Branch creation");
System.out.println(e);
}
答案 0 :(得分:2)
根据Git手册,您可以使用以下语法创建分支。它创建了一个名为<branchname>
的新分支头,它指向当前的HEAD,如果给定,则为<start-point>
:
git branch [--track | --no-track] [-l] [-f] <branchname> [<start-point>]
因此,您可以按照JGit中的类似语法创建本地分支“topic”(请参阅下一个代码块)。在这种情况下,您没有明确配置起始点。 JGit将使用HEAD作为起点。所以一切正常:
git.branchCreate().setName("topic").call();
但是,如果您使用起点origin/topic
创建本地分支,JGit将尝试在Git References中找到此引用。在此上下文中,origin
是远程的名称,topic
是分支的名称。如果没有找到,它会引发异常:
git.branchCreate().setName("topic").setStartPoint("origin/topic").call();
您还需要注意:
setForce(true)
会将目标分支名称重置为起始点。没有-f,--force
git branch拒绝更改现有分支。答案 1 :(得分:0)
SzilágyiIstván,我创建了如下分支:
try (Git git = new Git(repository)) {
Ref ref = git.branchCreate().setName("Name_Branch").setStartPoint("origin/develop").call();
git.checkout().setName(branch).call();
git.push().setCredentialsProvider(userService.getCredencialsProvider()).call();
logger.info("Branch created: " + ref + " - " + ref.getName() + " - " + ref.getObjectId().getName());
}