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:

  • I cloned a git repository. How do I pull from it into a working directory?
  • Git error fatal: unable to stat '*': No such file or directory
  • git repository browsing
  • How to enter command with password for git pull?
  • Redo merge of just a single file
  • git commit and push to branch but not to pull request
  • Branch: Ref[refs/heads/master=d766675da9e6bf72f09f320a92b48fa529ffefdc] refs/heads/master d766675da9e6bf72f09f320a92b48fa529ffefdc
    Number of branches: 1
    Branch: master
    

  • Permission Denied (publickey) - Git Bash on Windows
  • Why various git commands are available only locally?
  • git-stash changes without reverting
  • Looking for more info from my Git diff command
  • Is git dot notation in its commands a part of something more generic?
  • Git-windows case sensitive file names not handled properly
  • 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.