I am trying to find which local branch would have been the default when cloning a git repository.
In this particular context I am able to assume the remote is called origin. I can get the default branch for origin using git symbolic-ref refs/remotes/origin/HEAD. This returns something like refs/remotes/origin/develop.
I now need to find which local branches are set up to track origin/develop. Obviously it's almost certainly going to be develop, so I could just do some string manipulation to replace remotes/origin/ and end up with refs/heads/develop. However, I would like to avoid making that assumption.
I'm currently grepping it out of the result of git branch -vv (specifically git rev-parse --symbolic-full-name $(git branch -vv | grep "\[$(git rev-parse --abbrev-ref $(git symbolic-ref refs/remotes/origin/HEAD))\]" | awk '{print $1}')). However, this is very brittle, especially as the branch name can appear in the commit message.
I am currently using git 2.18, though could upgrade if newer verisons have a feature required to make this better.