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
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

  • By
    Element Position Swapping Using MooTools 1.2

    We all know that MooTools 1.2 can do some pretty awesome animations. What if we want to quickly make two element swap positions without a lot of fuss? Now you can by implementing a MooTools swap() method. MooTools 1.2 Implementation MooTools 1.2 Usage To call the swap...

  • By
    Google Font API

    Google recently debuted a new web service called the Font API.  Google's Font API provides developers a means by which they may quickly and painlessly add custom fonts to their website.  Let's take a quick look at the ways by which the Google Font...

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!