Take the 2-minute tour ×

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:

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.

share|improve this question
From your error it seems there is a problem with your refSpec. You sure it is correct? –  S.P. Aug 9 '12 at 22:18
add comment

4 Answers

As per regular git syntax, shouldn't your RefSpec() be: :branchToDelete?

share|improve this answer
Yes, either using new RefSpec(":branchToDelete") or new RefSpec().setSource("").setDestination("branchToDelete"). –  robinst Aug 10 '12 at 8:09
@Vince No, if the source is empty, it means that the target branch should be deleted on the remote. (That's what the question was about.) –  robinst Aug 10 '12 at 10:55
ok, I prefer to delete the comment to avoid mistakes then –  Vince Aug 10 '12 at 11:24
add comment

I never did it, but did you simply try a DeleteBranchCommand by specifying origin/branchToDelete?

EDIT : I particularly mean Git/JGit references remote branches via the structure <remote name>/<branch name> (and using the ListBranchCommand will help you make sure you got the correct spelling).

To know the exact spelling of the branch name, you can use a ListBranchCommand (don't forget to call setListMode(REMOTE)).

Note: Git allows more weird behaviours than JGit, so unless it is written somewhere, don't expect them.

EDIT : I mean that a refspec is supposed to have the following syntax: <remote branch>:<local branch> (or probably the other way around), but don't expect it works in JGit if you miss one end, even if it works in Git.

share|improve this answer
OP doesn't want to only delete the remote-tracking branch locally, but push the branch deletion instead. –  robinst Aug 10 '12 at 8:06
yeah, I got it. What I meant is you can display the remote branch (different from the local branch tracking it) and then delete the remote one. By the way, a refspec is made to specify the link between a local tracking branch and the remote one. I edited my answer for better understanding –  Vince Aug 10 '12 at 8:23
Brilliant! Passing origin/branchToDelete (or to be precise, refs/remotes/origin/branchToDelete) to the DeleteBranchCommand worked. The key thing of working with JGit: don't try to mimic GIT commands –  ivan.verhun Aug 13 '12 at 14:11
@Ivan, is this solution validated? or it does not work? –  Vince Aug 14 '12 at 12:01
Well, @Vince, after running the following code git.branchDelete().setBranchNames("refs/remotes/origin/" + branchToDelete).setForce(true).call(); the git branch -a doesn't contain entry for the branchToDelete That's why I decided that everything works fine. But when I run git pull, I was surprised to see * [new branch] test1 -> origin/test1, and on GitHub the branch is still showed. So it looks like the DeleteBranch command deleted only a local reference to the branch, but didn't affect the remote branch itself. I'm stuck again... –  ivan.verhun Aug 14 '12 at 21:24
show 2 more comments

This should do help you out:

//delete branch 'branchToDelete' locally

//delete branch 'branchToDelete' on remote 'origin'
RefSpec refSpec = new RefSpec()

tested with jgit

share|improve this answer
add comment

I could make it work with this:

StoredConfig config = git.getRepository().getConfig();
config.unsetSection("remote", "origin");
try {
} catch (IOException e) {

Hope it helps.

share|improve this answer
add comment

Your Answer


