← Winnitude! Search  ·  Reader Mode (1800 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 24 LINKS

How to know which is the default branch · gitpython-developers/GitPython · Discussion #1364

Repository navigation

How to know which is the default branch #1364

Answered by richtier dsharma522 asked this question in Q&A How to know which is the default branch #1364 dsharma522 Oct 20, 2021 · 3 comments · 5 replies Answered by richtier Return to top Discussion options

dsharma522 Oct 20, 2021

How one can one which is the default branch? main/master?

One can know about this by iterating all heads and then manually check , but that is also have one issue.
What if a repository has both the branches as their branch, which means master, main are some branch and default branch could be one of these or something else.

How to get the default branch ?

2 You must be logged in to vote Answered by richtier Nov 19, 2021

this seems to work repo.remotes.origin.refs.HEAD.ref.remote_head

bit verbose tho!

View full answer

Replies: 3 comments 5 replies

Comment options

Byron Oct 20, 2021
Maintainer

The default branch is only relevant once, when initializing the repository and git has to decide how to name the only branch in the repository. From there the only questions that can be answered are...

  • which branch is currently checked out
  • which remote is associated with a branch
1 You must be logged in to vote 4 replies Comment options

dsharma522 Oct 25, 2021
Author

Can we rely upon

repo = git.Repo('repo')
name = repo.heads[0]
print(name)

Will it give the main branch of the repo (main/master or any branch which is main of the repo)

1 You must be logged in to vote 0 replies Comment options

richtier Nov 19, 2021

this seems to work repo.remotes.origin.refs.HEAD.ref.remote_head

bit verbose tho!

Marked as answer 1 You must be logged in to vote 1 reply Answer selected by Byron Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment Category Q&A Labels None yet 4 participants