Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove remote branch with JGit

I'm not able to figure out how to remove a remote branch.

I was trying to mimic the following GIT command: git push origin :branchToDelete

The following code and it's variations with the empty source:

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

throws and exception like:

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

Thanks in advance for your ideas and solutions.

like image 545
ivan.verhun Avatar asked Aug 09 '12 22:08

ivan.verhun


1 Answers

This should help you out:

//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();

tested with jgit 2.0.0.201206130900-r

like image 186
Pieter Avatar answered Sep 30 '22 02:09

Pieter