Search code examples
gitbranchgit-remote

How do you find the default branch of a remote git repository?


Different repositories name their primary development branches differently (e.g. main, gh-pages, master, etc.), and scripts meant to work with multiple repositories need an automated way to discover the current repository's default branch name.

What git command returns that name?


Solution

  • git rev-parse --abbrev-ref origin/HEAD will print origin/<default-branch-name>.

    If the origin repository changes its default branch name, then git remote set-head origin -a will retrieve the new default branch name.