Read Git Default Branch from the Command Line
Jonathan Bowman Created: January 29, 2021 Updated: July 06, 2023 [Dev] #git #commandlineOn 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 sed
:
|
If you have jq
it is even simpler:
|
Or for those who prefer curl
:
|
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 default_branch
key.
Is this something you might use repeatedly? Consider building a function and placing the following in your shell config (such as .bashrc
):
Feel free to use this gist
Or, for Windows Powershell, place the following in your Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
:
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 master
.
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 git ls-remote
.
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 $REPO_URL
:
|
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:
|
In Powershell:
(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
Happy scripting!