Philip Oakley
Philip Oakley

Reputation: 14061

command to determine the upstream ref of the current HEAD?

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)

Upvotes: 8

Views: 1506

Answers (1)

Jonathan Wakely
Jonathan Wakely

Reputation: 171253

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").

Upvotes: 16

Related Questions