How to Detect the Default Branch in a git Repository

By  on  

Over the past few years, many engineering teams have switched their default git branch name from master to a different, potentially less offensive term. I'm all for choosing to name your default branch whatever you'd like, but not having a universal default branch name can complicate some automation.

So how can we detect the default branch name for a git repository? I use a few chained commands:

git remote show REMOTE_REPO_NAME | grep 'HEAD branch' | cut -d' ' -f5

Swap out REMOTE_REPO_NAME with the name of the remote/ upstream repository and you'll get the remote repository's default branch name!

Recent Features

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

Incredible Demos

  • By
    Checkbox Filtering Using MooTools ElementFilter

    When I first wrote MooTools ElementFilter, I didn't think much of it. Fast forward eight months later and I've realized I've used the plugin a billion times. Hell, even one of the "big 3" search engines is using it for their maps application.

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Discussion

  1. Djangounet

    Cool trick ! Except… it works only if your LANG is “en”…

    My attempt :

    git remote show origin | grep 'HEAD' | cut -d':' -f2 | sed -e 's/^ *//g' -e 's/ *$//g'
    

    Best regards

  2. WA

    This one should be language-neutral:

    git ls-remote --symref https://github.com/cli/cli HEAD | awk -F'[/\t]' 'NR == 1 {print $3}'
    
    
  3. sp

    Hi,
    How to find default branch for all the repositories in an organization ?

  4. Alex Z

    Hi, thanks for it!

    I digged a bit further on git remote and I noticed in its man page the subcommand git remote set-head. Its description begins with “Sets or deletes the default branch (i.e. the target of the symbolic-ref refs/remotes//HEAD)”.

    So, it turns out that we can actually do this:

    $ sed -e ‘s/^.*\///’ < .git/refs/remotes/origin/HEAD
    devel

    It is way faster than actually querying the remote server.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!