Getting all branches with JGit

How can I get all branches in a repository with JGit?
Let’s take an example repository. As we can see, it has 5 branches.
Here I found this example:

int c = 0;
List<Ref> call = new Git(repository).branchList().call();
for (Ref ref : call) {
    System.out.println("Branch: " + ref + " " + ref.getName() + " "
            + ref.getObjectId().getName());
    c++;
}
System.out.println("Number of branches: " + c);

But all I get is this:

  • Re-doing a reverted merge in Git
  • Why is git rebase discarding my commits?
  • Undo multiple file and folder 'git add'
  • git diff --cached: refname 'HEAD' is ambiguous
  • “git checkout — *” returns “error: pathspec did not match any files known to git”
  • Installing Openstack errors
  • Branch: Ref[refs/heads/master=d766675da9e6bf72f09f320a92b48fa529ffefdc] refs/heads/master d766675da9e6bf72f09f320a92b48fa529ffefdc
    Number of branches: 1
    Branch: master
    

  • How do I change the external editor for GitHub client on MAC or Windows?
  • How to avoid providing absolute path of git repo while cloning?
  • What goes into your .gitignore if you're using CocoaPods?
  • How to push specific commit of branch to other branch after many commits are already done on both branches
  • Virtual env: Connection in use error
  • Eclipse subversive on dev box issue
  • One Solution collect form web for “Getting all branches with JGit”

    If it is the remote branches that you are missing, you have to set the ListMode of the ListBranchCommand to ALL or REMOTE. The default ListMode (null) returns only local branches.

    new Git( repository ).branchList().setListMode( ListMode.ALL ).call();
    
    Git Baby is a git and github fan, let's start git clone.