Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - show remote name of remote branch

Tags:

git

shell

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.

like image 571
try-catch-finally Avatar asked Dec 13 '25 02:12

try-catch-finally


2 Answers

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.

like image 178
Etan Reisner Avatar answered Dec 14 '25 15:12

Etan Reisner


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
like image 25
Martijn Pieters Avatar answered Dec 14 '25 15:12

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!