I'm writing a script that will delete remote branches that are already merged into the default remote branch, but I'm having trouble determining the remote default branch because it isn't always set.
I know if I clone a repo, then I get a tracking branch to
As alexbclay implied in a comment, there seems to be no real use for this, but there are several ways to find it:
If your Git is new enough (and the one on the server is as well),
git ls-remote --symref will print it out (plus everything else, so you might want to add more options to limit the output):
$ git ls-remote --symref ... ref: refs/heads/master HEAD ...
Once you have this, you can create the reference locally with
git symbolic-ref (in this case, assuming remote
git symbolic-ref refs/remotes/foo/HEAD refs/remotes/foo/master: follow the usual renaming, using
git config --get-all remote.foo.fetch to figure out the renaming, if it's non-standard).
If that fails, you can fall back on the method
git clone uses: read the remote heads (with
git ls-remote again) and see if there is a unique match on the ID for
HEAD against all the actual branch heads. If it's unique, you know which one it is. If not, you can pick the first alphabetically as
git clone does, or pick one at random. (I'm not sure off-hand what
git clone does if the remote's
HEAD is detached and thus does not match any of the remote's branches. It might be interesting to find out.)
Slow but simple: make a new clone, see what you get for
refs/remotes/origin/HEAD. :-) That's basically the previous two methods glommed together. (Use
--reference to speed up the network interaction, perhaps.)