简体   繁体   中英

Retrieving Branches from GIT url using java code

Currently I'm working on Jgit API to create a branch from source code. I have created the branch successfully and when I create branch I need to validate the branch name shouldn't exists in the current GIT repositary(Online URL : Ex. https://github.com/varunkumar-s/test.git ).

Here I want to do list out the branch names from the above url rather from the local git repository. So how can i do this?? I want to have a output of list of branches when I'm giving with the git repository.

The jgit-cookbook has a Snippet for this at ListRemoteRepository , basically all you need is:

    Collection<Ref> refs = Git.lsRemoteRepository()
            .setHeads(true)
            .setRemote(REMOTE_URL)
            .call();

    for (Ref ref : refs) {
        System.out.println("Ref: " + ref);
    }

From the ref you can use ref.getName() to get the name which will be something like "refs/heads/".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM