1

How one can know about the master/main branch at git remote with git-python.

I am aware that we can iterate over the heads of the repository and then check the results. Something like

repo = git.Repo('git-repo')
remote_refs = repo.remote().refs

for refs in remote_refs:
    print(refs)

This will give list of all branch heads at remote including the main/master branch.

Is their a direct way to get the main branch?

John Byro
  • 674
  • 3
  • 13

1 Answers1

1

Git itself doesn't have a concept of a "main branch". It has a default name for the first branch that is created but you can rename it as you wish, create more branches from the first commit and let them all head in their own directions.

The concept of a "main branch" is really only relevant in the central repository management systems such as GitHub, GitLab and Bitbucket.

So if you want the branch that is considered the "main branch" by those systems, you'd have to use their API to query the name of that branch for the project in question.

So the answer is no, there is no way to find the "default branch" with git-python, as there isn't even a way to do that with git itself.

StFS
  • 1,639
  • 2
  • 15
  • 31