0

I want a list of all the remote branches for a git repo. Now, this git repo can be private / public. I have the access to the token to access the repo.

I am using this particular SDK : https://pkg.go.dev/github.com/go-git/go-git/v5

One way to do this is ..

r, cloneErr := git.PlainClone(projectRoot, false, cloneOptions)
remote, err := r.Remote("origin")
if err != nil {
    panic(err)
}
refList, err := remote.List(&git.ListOptions{})
if err != nil {
    panic(err)
}
refPrefix := "refs/heads/"
for _, ref := range refList {
    refName := ref.Name().String()
    if !strings.HasPrefix(refName, refPrefix) {
        continue
    }
    branchName := refName[len(refPrefix):]
    fmt.Println(branchName)
}

But, this involves cloning the repo first. How can I get the list without cloning the repo ?

Thanks in advence !

torek
  • 448,244
  • 59
  • 642
  • 775
Keval Bhogayata
  • 4,422
  • 3
  • 13
  • 36
  • => https://stackoverflow.com/help/minimal-reproducible-example – jub0bs Dec 08 '21 at 09:48
  • 3
    `git ls-remote --heads` lists all heads (branches) on a given remote even if you've never ran `fetch` on the local repo. If your library has an API equivalent to that, it should be enough for your needs. – kalatabe Dec 08 '21 at 09:53
  • 2
    I don't really know go but this seems to be the appropriate method: https://pkg.go.dev/github.com/go-git/go-git/v5#Remote.List – kalatabe Dec 08 '21 at 10:02
  • @kalatabe, I have mentioned the same method in the code snippet, but my goal was to somehow skip the Plainclone process. I am able to do it now with "NewRemote" method – Keval Bhogayata Dec 08 '21 at 10:19

0 Answers0