git - is there a reference to upstream branch?

278 views Asked by At

I can ask for differences since I started on a feature branch:

git diff upstream-branch...

But it requires that I know what the upstream branch is. Is there a reference for the upstream branch, whatever it may be without me knowing before hand or having to dig for it? Like, I don't know:

git diff UPSTREAM...
1

There are 1 answers

2
torek On BEST ANSWER

As a one liner: git diff @{u}...


The upstream of a branch is composed of two parts, both of which can be set and retrieved with git config. The remote part is easy, as given a branch named B, it's branch.B.remote. The second half is much harder if you use git config.1 Fortunately, since about Git version 1.8 or so, the @{upstream} suffix works for all things that parse branch names:

foo@{upstream}

is the upstream of branch foo. @{u} is shorthand for @{upstream}, and standalone, means HEAD@{upstream}.

To get the symbolic name of the upstream, if that's what you need, use git rev-parse --symbolic-full-name or git rev-parse --abbrev-ref. Note that if there is no upstream set for the current or given branch, you'll get an error from git rev-parse.


1It's branch.B.merge, but this must be run through the fetch = mappings for the given remote to find the right remote-tracking name. That is, suppose that branch br has branch.br.remote = r and branch.br.merge = xyz. You must then run refs/heads/xyz through the remote.r.fetch rules to come up with the remote-tracking name corresponding to xyz on remote r. There's no command-line command that will do this for you.