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?
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With