Delete branch with jgit not working as expected

I am trying to delete a branch in my repo using jgit.

DeleteBranchCommand command = git.branchDelete();
try {
    List<String> deletedBranches = new ArrayList<String>();
deletedBranches =;
} catch (GitAPIException e) {
throw new MyInternalErrorException("Error while deleting branch [" + branchName + "]", e);

The value of deletedBranches will be [myBranch]

  • git fetch doesn't fetch all branches
  • Can a ref stored outside refs/heads be considered a branch and checked-out and worked-on as a normal branch?
  • best way to versionize different git branches
  • Can I recover a branch after its deletion in Git?
  • how can I figure out when a branch in git was merged into the trunk
  • How to get git branch of colleague who was fired
  • If I check if the branch is still in the repo:


    I will get true.
    And this is because cf to jgit javadoc:


    name the name of the ref to lookup. May be a short-hand form, e.g.
    “master” which is is automatically expanded to “refs/heads/master” if
    “refs/heads/master” already exists.

    It is checking for “refs/heads/myBranch” instead of “myBranch”.

    Moreover, if I will run the deleteBranch command the second time, the value for deletedBranches will be [refs/heads/myBranch].

    Can someone explain why is this happening and how can I solve this issue?
    Thank you.


    After debugging inside the jgit code, I’ve noticed that

    String fullName = currentRef.getName();

    returns “myBranch” instead of “heads/refs/myBranch” thus it is not executing this piece of code:

                    if (fullName.startsWith(Constants.R_HEADS)) {
                        String shortenedName = fullName
                        // remove upstream configuration if any
                        final StoredConfig cfg = repo.getConfig();

  • Best way to incorporate external django app into a project and safely make local changes
  • Will git push commits from all branches automatically?
  • Git Development vs Release branch best practices
  • Clean git repo on Heroku
  • User/Repository Hierarchy in GIT
  • Babel not working in git hook
  • One Solution collect form web for “Delete branch with jgit not working as expected”

    It could be similar to git branch -d, which will refuse to delete a branch not fully merged in its upstream branch.

    That is why the examples you see in org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest are using setForce(true) (like a git branch -D):

    List<String> deleted = git.branchDelete().setBranchNames("ForDelete")
    Git Baby is a git and github fan, let's start git clone.