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
Is there a git command available to determine default branch for remote repository?
No, there doesn't seem to be:
git ls-remote -v https://github.com/<user>/<repo>
That would list all branches, but not HEAD (which is the symref which designates the default branch)
Similarly, the GitHub Reference API can list heads, but would not include HEAD as well.
There is a --short option to git symbolic-ref. So my preferred command:
$ git symbolic-ref --short HEAD
master
This works for me with Git 2.1.10, using a repository cloned from GitHub:
git branch -r --points-at refs/remotes/origin/HEAD
A major problem with this approach is that it lists every remote branch pointing to HEAD; however, the output includes a hint:
origin/HEAD -> origin/master
origin/master
origin/test123
So you can post-process the output with grep
or similar to find the one with the arrow:
git branch -r --points-at refs/remotes/origin/HEAD | grep '\->' | cut -d' ' -f5 | cut -d/ -f2