【发布时间】:2022-01-05 02:44:29
【问题描述】:
我正在尝试使用 jgit 创建一个远程分支,它完全执行以下 git 命令:
- git clone git@gitlab.com:my-project/test.git
- git checkout -b superBranch
- git push --set-upstream origin superBranch
在这些执行之后,我可以在没有合并请求的情况下更改和推送分支的文件。
jGit:
不幸的是 jgit 不知道命令“push -u”(上游)。所以我找到了一些可能的解决方案。但并不是所有的解决方案都真的有效。
// git clone done, than:
git.branchCreate()
.setName("superBranch")
.setForce(true)
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
.setStartPoint("origin/superBranch").call(); // <-- Ref not found
RefSpec refSpec = new RefSpec().setSourceDestination("superBranch", "superBranch");
git.push()
.setRefSpecs(refSpec)
.setCredentialsProvider(provider).call();
git.checkout().setName("superBranch").call();
异常:
org.eclipse.jgit.api.errors.RefNotFoundException: Ref origin/superBranch cannot be resolved
我在这里找到的另一个解决方案Eclipse Forum:
git.branchCreate().setName("superBranch").call();
git.push()
.setRemote("origin")
.setRefSpecs(new RefSpec("superBranch" + ":" + "superBranch")) //<-- Ref not found
.setCredentialsProvider(provider).call();
git.branchCreate()
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM)
.setStartPoint("origin/" + "superBranch")
.setForce(true).call();
git.checkout().setName("superBranch").call();
异常:
org.eclipse.jgit.api.errors.InvalidRefNameException: Branch name <null> is not allowed
有谁知道我如何创建远程和本地分支,而无需调用 api 或像顶部的 git 示例那样发出合并请求?
【问题讨论】:
-
我认为您需要使用完全限定的参考名称。在你的第一个 sn-p 中,这将是
refs/remotes/origin/superBranch创建分支和refs/heads/superBranch:refs/remotes/origin/superBranch推送。 -
@RüdigerHerrmann 感谢您的回复。如果我使用完全限定的 ref 名称,那么我会收到相同的异常“RefNotFoundException:无法解析 Ref refs/remotes/origin/superBranch”
-
看起来 jgit 正在重复 Git 的伎俩,即拒绝您使用远程跟踪名称(在本例中为
refs/remotes/origin/superBranch)直到它真正存在。这并非完全不合理,尽管有一些方法可以绕过它,就像在 Git 中一样。 -
还值得一提的是,“远程分支”这个短语要么是荒谬的,要么是模棱两可的:它是指
superBranch,因为它存在于另一个(远程)Git repo 上,还是指@987654331 @ 因为它存在于您自己的 Git 存储库中?因此,我使用术语 remote-tracking name,这显然意味着您的存储库中的那个。