gitgithubbranch

How to get default Git branch?


My team alternates between usage of dev and master as default branch for several repos and I would like to write a script that checks for the default branch when entering a directory.

When pull requests are opened in some of these repos, they either default to 'dev' or 'master' as the merge target.

I understand how to set this information but not retrieve it: https://help.github.com/articles/setting-the-default-branch/

Is there a git command available to determine default branch for remote repository?


Solution

  • Tested with git 2.9.4 (but possibly works in other versions) in a repo cloned from Github:

    git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'
    

    Sample output:

    master
    

    Or:

    git symbolic-ref refs/remotes/origin/HEAD --short
    

    Sample output:

    origin/master