git - how to get default branch?
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
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)
git rev-parse --abbrev-ref origin/HEAD
will print origin/<default-branch-name>
. The git symbolic-ref
answers are doing the same thing but need a longer argument.
If the origin
repository changes its default branch name, then git remote set-head origin -a
will retrieve the new default branch name.