如何使用JGit删除远程分支

iva*_*hun 2 java git remote-branch jgit

我无法弄清楚如何删除远程分支.

我试图模仿以下GIT命令:git push origin:branchToDelete

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

RefSpec refSpec = new RefSpec();
refSpec = refSpec.setSource("");
// remove branch from origin:
git.push().setRefSpecs(refSpec).add(branchToDelete).call();
Run Code Online (Sandbox Code Playgroud)

投掷和例外如:

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
Run Code Online (Sandbox Code Playgroud)

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

Pie*_*ter 7

这应该可以帮到你:

//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();
Run Code Online (Sandbox Code Playgroud)

用jgit 2.0.0.201206130900-r测试


Jon*_*Lin 3

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