Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

command to determine the upstream ref of the current HEAD?

Tags:

git

git-branch

I'm looking for what I hope is the simple one line command to determine the the correct upstream ref for the currently checked out branch?

Essentially something like

git branch --remote HEAD

which (if it worked) would convert the symbolic pattern HEAD to the current branch name, and then the option --remote then changes it to the ref of the remote-tracking branch. (But it doesn't do that!)

If I have branch morehelp with a config of

remote = origin
merge = refs/heads/morehelp

The simple command line would return refs/remotes/origin/morehelp which is it's upstream tracking branch (ideal for the git reset --hard <ref> case of an update by overwrite)

like image 665
Philip Oakley Avatar asked Mar 07 '13 23:03

Philip Oakley


1 Answers

I think you want

git rev-parse --symbolic-full-name @{u}

@{u} is the abbreviation for the upstream tracking branch of HEAD and the option tells rev-parse to print it in the format you want, rather than printing an SHA commit ID.

From git help rev-parse

   --symbolic
       Usually the object names are output in SHA1 form (with possible ^ prefix); this option makes them output in a form as close to the original
       input as possible.

   --symbolic-full-name
       This is similar to --symbolic, but it omits input that are not refs (i.e. branch or tag names; or more explicitly disambiguating
       "heads/master" form, when you want to name the "master" branch when there is an unfortunately named tag "master"), and show them as full
       refnames (e.g. "refs/heads/master").
like image 170
Jonathan Wakely Avatar answered Sep 21 '22 08:09

Jonathan Wakely