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
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

Incredible Demos

  • By
    CSS Fixed Positioning

    When you want to keep an element in the same spot in the viewport no matter where on the page the user is, CSS's fixed-positioning functionality is what you need. The CSS Above we set our element 2% from both the top and right hand side of the...

  • By
    Add Styles to Console Statements

    I was recently checking out Google Plus because they implement some awesome effects.  I opened the console and same the following message: WARNING! Using this console may allow attackers to impersonate you and steal your information using an attack called Self-XSS. Do not enter or paste code that you...

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!