Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git - how to get default branch?

Tags:

git

branch

github

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?

like image 844
lfender6445 Avatar asked Feb 23 '15 03:02

lfender6445


People also ask

What is the default branch in git?

A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master . As you start making commits, you're given a master branch that points to the last commit you made.


2 Answers

I found a way to detect the default-branch if it is not master.

git remote show [your_remote] | sed -n '/HEAD branch/s/.*: //p' 

I tested it with multiple repo from gitlab, and it worked fine. (for the most situations [your_remote] will be origin, run git remote to check the name of your remote)

like image 190
Radon8472 Avatar answered Sep 29 '22 23:09

Radon8472


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/@@' master 
like image 43
danielkza Avatar answered Sep 29 '22 21:09

danielkza