Is there a Git command to show the remote name of a remote branch?
Currently I stick with extracting the remote name from a remote branch ref using shell utils like ...
$ echo "remote-name/branch-name" | sed -r 's;^([^/]+)/.*;\1;'
remote-name
sometimes passing it through for validation purposes
$ git rev-parse --symbolic-full-name "remote-name/branch-name" | ...
I wonder if there's a builtin command like.
$ git wanted-command [wanted options] "remote-name/branch-name"
remote-name
I've looked though the manpages of rev-parse, remote and ls-remote.
Not the prettiest solution but I think it should generally work. (I feel like there should be a better way but I can't think of it at the moment.)
symref=$(git symbolic-ref --short HEAD)
git config --local "branch.$symref.remote"
And, for completeness and to sum-up some of the comments below, the name of the tracking branch on the remote (which may not be the same as the name remote ref in the local repository) is available from branch.$symref.merge.
If instead of the remote reference, you started with a local reference, you could use git for-each-ref with the %(upstream:remotename) format.
I use this together with git symbolic-ref -q HEAD to get the remote name for the current checkout:
remote=$(git for-each-ref --format='%(upstream:remotename)' "$(git symbolic-ref -q HEAD)")
Demo with the most common remote:
% git for-each-ref --format='%(upstream:remotename)' "$(git symbolic-ref -q HEAD)"
origin
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