GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — How to know which is the default branch · gitpython-developers/GitPython · Discussion #1364

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 dsharma522
Oct 20, 2021 · 3 comments · 5 replies
Answered by richtier Return to top
Discussion options
  • Uh oh!

    There was an error while loading. Please reload this page.

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 ?

You must be logged in to vote
All reactions
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
  • Uh oh!

    There was an error while loading. Please reload this page.

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
You must be logged in to vote
All reactions
4 replies
@dsharma522
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

dsharma522 Oct 21, 2021
Author

Is there any way to know the default_branch of repository?

All reactions
@Byron
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

Byron Oct 22, 2021
Maintainer

There is a way to ask for configuration values, search for 'config' here. From there it's, it's using the right configuration key to get or set the value.

All reactions
@dsharma522
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

dsharma522 Oct 22, 2021
Author

I did repo.config_reader() with all possible config levels (system, global,repository) , it outputs ['DEFAULT'] for every case. Tested with many repositories

All reactions
@Byron
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

Byron Oct 23, 2021
Maintainer

Keep debugging and I am sure it will be possible to read config values from it. If that's not the case and reproducible please file an issue.

All reactions
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

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)

You must be logged in to vote
All reactions
0 replies
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

richtier
Nov 19, 2021

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

bit verbose tho!

You must be logged in to vote
All reactions
1 reply
@agowa
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

agowa Oct 8, 2024

This doesn't work when refs/remotes/origin/HEAD has not yet been created.
With the git binary the correct command would be git remote set-head origin -a. Does anyone know what the GitPython equivalent to this is?

All reactions
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
@dsharma522 @Byron @agowa @richtier
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.