Read Git Default Branch from the Command LineJonathan Bowman Created: January 29, 2021 Updated: July 06, 2023 [Dev] #git #commandline
On occasion, one needs to know the default branch for a given Git repo. Below I have compiled methods that fit a variety of use cases, and cover specific platforms (Github and Gitlab) as well as methods that work universally regardless of remote platform.
🔗Github API method
Given a repo
username/reponame, the following script will work in Bash, Dash/Ash, or Zsh, provided you have the
wget tool, and
If you have
jq it is even simpler:
Or for those who prefer
This is pretty easy in Powershell, as well:
iwr -useb https://api.github.com/repos/username/reponame | ConvertFrom-Json | select -exp default_branch
In summary, we first query information about the given repo, then parse the JSON that is returned, extracting the value of the
Is this something you might use repeatedly? Consider building a function and placing the following in your shell config (such as
Feel free to use this gist
Or, for Windows Powershell, place the following in your
Feel free to use this gist
🔗Similar functionality on Gitlab
The above is specific to Github, because it is specifically a Github setting. In Github, even an empty repo can have a default branch that is different than
Gitlab, another popular “social coding” service, has different but similar functionality. A populated repo can have a default branch. An empty one will not.
Here is an example function that uses the Gitlab API (see gist):
and the Powershell equivalent:
🔗A better, universal approach if repo is non-empty
My original intent was to read a Github setting on a repo, new or old, empty or not. However, if desiring to simply get the default branch on an existing repo, an API call is unnecessary. We can use
Thanks to u/Cyberbeni in this Reddit discussion for these suggestions.
To query the default branch on a remote repository, try this, with your repository URL assigned to or substituted for
Or Powershell equivalent:
"$(git ls-remote --symref 'https://gitlab.com/bowmanjd/dotfiles1.git' HEAD)" -replace '.*?^ref: refs/heads/(\S+).+','$1'
🔗Get local origin/HEAD reference if available
If the repo was downloaded with
git clone, then
origin/HEAD is set automatically. In such cases, the following should work quickly, without needing to query the remote:
(git symbolic-ref refs/remotes/origin/HEAD) -split '/' | select -Last 1
Please note the above is unreliable, as it is easily possible to work locally without
origin/HEAD being set.
Again, thanks to u/Cyberbeni for the tip on Reddit.
🔗Get locally-set Git preference
Of course, if you just want to know the current user’s preferred default branch, the following will yield the results, or blank if none has been set up:
One can set this value to, for instance,
main with something like
Back to top