首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JGit删除远程分支

如何使用JGit删除远程分支
EN

Stack Overflow用户
提问于 2012-08-10 06:15:13
回答 4查看 5.5K关注 0票数 6

我不知道如何删除远程分支。

以下代码及其与空源代码的变体:

代码语言:javascript
复制
RefSpec refSpec = new RefSpec();
refSpec = refSpec.setSource("");
// remove branch from origin:
git.push().setRefSpecs(refSpec).add(branchToDelete).call();

抛出和异常,如:

代码语言:javascript
复制
org.eclipse.jgit.api.errors.JGitInternalException: Exception caught during execution of push command
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:175)
    at org.gitscripts.DeleteBranchOperation.execute(DeleteBranchOperation.java:27)
    at org.gitscripts.Main.main(Main.java:27)
Caused by: java.io.IOException: Source ref  doesnt resolve to any object.
    at org.eclipse.jgit.transport.RemoteRefUpdate.<init>(RemoteRefUpdate.java:285)
    at org.eclipse.jgit.transport.RemoteRefUpdate.<init>(RemoteRefUpdate.java:189)
    at org.eclipse.jgit.transport.Transport.findRemoteRefUpdatesFor(Transport.java:612)
    at org.eclipse.jgit.transport.Transport.findRemoteRefUpdatesFor(Transport.java:1150)
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:149)
    ... 2 more

提前感谢您的想法和解决方案。

EN

回答 4

Stack Overflow用户

发布于 2012-09-12 17:35:33

这应该会对你有所帮助:

代码语言:javascript
复制
//delete branch 'branchToDelete' locally
git.branchDelete().setBranchNames('refs/heads/branchToDelete').call();

//delete branch 'branchToDelete' on remote 'origin'
RefSpec refSpec = new RefSpec()
        .setSource(null)
        .setDestination("refs/heads/branchToDelete");
git.push().setRefSpecs(refSpec).setRemote("origin").call();

测试jgit 2.0.0.201206130900-r

票数 14
EN

Stack Overflow用户

发布于 2012-08-10 13:16:43

按照常规的git语法,你的RefSpec()不应该是::branchToDelete

票数 3
EN

Stack Overflow用户

发布于 2012-08-10 15:16:09

我从来没有这样做过,但是您只是通过指定origin/branchToDelete来尝试DeleteBranchCommand吗?

编辑:我特别指的是Git/JGit通过结构<remote name>/<branch name>引用远程分支(使用ListBranchCommand可以帮助你确保拼写正确)。

要了解分支名称的确切拼写,可以使用ListBranchCommand (不要忘记调用setListMode(REMOTE))。

注意: Git允许比JGit更奇怪的行为,所以除非它是在某个地方写的,否则不要期待它们。

编辑:我的意思是,refspec应该有以下语法:<remote branch>:<local branch> (或者可能相反),但是如果你错过了一端,就不要指望它在JGit中工作,即使它在Git中工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11892766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档