How to remove remote branch with JGit
Asked Answered
B

4

7

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.

Breathless answered 9/8, 2012 at 22:15 Comment(1)
From your error it seems there is a problem with your refSpec. You sure it is correct?Enforcement
S
17

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

Swimmingly answered 12/9, 2012 at 9:35 Comment(0)
H
3

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

Henriettehenriha answered 10/8, 2012 at 5:16 Comment(2)
Yes, either using new RefSpec(":branchToDelete") or new RefSpec().setSource("").setDestination("branchToDelete").Harm
@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.)Harm
B
2

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.

Beluga answered 10/8, 2012 at 7:16 Comment(7)
OP doesn't want to only delete the remote-tracking branch locally, but push the branch deletion instead.Harm
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 understandingBeluga
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 commandsBreathless
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...Breathless
you can try to run a git push after you delete the branch. I mean, Git is a two-step tool, with which you first record your changes locally and then to the remote repository. So I think you should delete the tracking branch or the remote branch and then push that.Beluga
it's interesting though, how would Git react if you run git branch -d branchToDelete and then git push origin, with branchToDelete being a remote-tracking branch?Beluga
Nothing will happen, you still just deleted the remote tracking branch in your local repository.Collayer
D
0

I could make it work with this:

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

Hope it helps.

Davena answered 9/10, 2012 at 12:15 Comment(1)
He wants to delete a branch on the remote, not the remote from the local configuration.Collayer

© 2022 - 2024 — McMap. All rights reserved.