jgit-cookbook icon indicating copy to clipboard operation
jgit-cookbook copied to clipboard

delete remote branch

Open accfcx opened this issue 4 years ago • 2 comments

How to delete remote branch with JGit? I search over Google and text following code.

   git.branchDelete()
            .setBranchNames(fullBranch)
            .call();

    RefSpec refSpec = new RefSpec()
            .setSource(null)
            .setDestination(fullBranch);

    git.push()
            .setRefSpecs(refSpec)
            .setRemote("origin")
            .call();

but it does not work at all. It just deletes local branch.

accfcx avatar Mar 24 '20 14:03 accfcx

I fear the Git protocol does not allow this, you may need to delete the branch locally and then push that deletion to the remote repository.

However I am not sure about how to do this actually, sorry. Please also note the statement in the README and in new-issue screen, duplicated below again.

Therefore I am closing this, please ask on stackoverflow to get more people to take a look and comment.

Note: Please use sites such as http://stackoverflow.com for general questions about JGit usage, not issues in this project. Issues should be used for problems with snippets and suggestions of missing snippets. Snippets from good answers on stackoverflow can then be included here, naturally.

centic9 avatar Apr 16 '20 20:04 centic9

The idea of snippet presented in this issue works well. I got the same from:

https://stackoverflow.com/questions/11892766/how-to-remove-remote-branch-with-jgit


I guess https://github.com/centic9/jgit-cookbook/issues/74#issue-587006012 the value of fullBranch variable is not correct.

If the branch is named testbranch, what is presented in CreateAndDeleteBranch.java in this project will delete the local branch:

https://github.com/centic9/jgit-cookbook/blob/288825e094e4deb7e8d67957cfe3b57f41c482f2/src/main/java/org/dstadler/jgit/porcelain/CreateAndDeleteBranch.java#L52-L55

Then this will delete the local pointer we have of the branch in the distant repository:

git.branchDelete()
    .setBranchNames("refs/remotes/origin/testbranch")
    .setForce(true)
    .call();

and this will delete the branch remotely in the distant repository:

    RefSpec refSpec = new RefSpec()
            .setSource(null)
            .setDestination("refs/heads/testbranch");

    git.push()
            .setRefSpecs(refSpec)
            .setRemote("origin")
            .call();

In my opinion a snippet of this cook book could contain and example for this last use-case. Can this issue be reopened?

jmini avatar Nov 29 '21 08:11 jmini