threads / discuss / 25203

get-upstream

Subject: get-upstream

## tl;dr

7 messages between Sep 23, 2010 and Sep 23, 2010.

replies: 6people: 3as markdown or json

Ramana Kumar· Sep 23, 2010, 03:46 UTC · lore

Is there an easy command to get a symbolic reference to the upstream branch of a tracking branch?

For example, to get the current branch name I can do this:

$ git symbolic-ref HEAD refs/heads/mybranchname

Now I can see the upstream name as follows

$ git branch -vv branch1 ... mybranchname sha [upstreamname: ahead n] ... branch3 ...

My question is is there a way to get upstreamname directly to stdout, similar to the symbolic-ref command?

(I don't want to have to type it manually; tab completion helps a little, but looking for more. Plus this would be necessary to avoid scraping in a script.)

Pat Notz· Sep 23, 2010, 04:05 UTC · re: Ramana Kumar · lore

Re: get-upstream

$ git rev-parse --symbolic-full-name @{upstream}
On Wed, Sep 22, 2010 at 9:46 PM, Ramana Kumar <ramana.kumar@gmail.com> wrote:
Show 26 quoted lines
> Is there an easy command to get a symbolic reference to the upstream
> branch of a tracking branch?
>
> For example, to get the current branch name I can do this:
>
> $ git symbolic-ref HEAD
> refs/heads/mybranchname
>
> Now I can see the upstream name as follows
>
> $ git branch -vv
> branch1 ...
> mybranchname sha [upstreamname: ahead n] ...
> branch3 ...
>
> My question is is there a way to get upstreamname directly to stdout,
> similar to the symbolic-ref command?
>
> (I don't want to have to type it manually; tab completion helps a
> little, but looking for more. Plus this would be necessary to avoid
> scraping in a script.)
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
Pat Notz· Sep 23, 2010, 04:15 UTC · re: Pat Notz · lore

Re: get-upstream

On Wed, Sep 22, 2010 at 10:05 PM, Pat Notz <patnotz@gmail.com> wrote:
> $ git rev-parse --symbolic-full-name @{upstream}
In all fairness, the @{upstream} syntax requires git >= 1.7.0
http://www.kernel.org/pub/software/scm/git/docs/RelNotes-1.7.0.txt
Ramana Kumar· Sep 23, 2010, 04:16 UTC · re: Pat Notz · lore

Re: get-upstream

Thanks Pat - that's awesome. Is there a git shortcut for removing the refs/remotes or refs/heads prefix? (I know I can just use other progs for that too)

On Thu, Sep 23, 2010 at 2:15 PM, Pat Notz <patnotz@gmail.com> wrote:
Show 7 quoted lines
> On Wed, Sep 22, 2010 at 10:05 PM, Pat Notz <patnotz@gmail.com> wrote:
>> $ git rev-parse --symbolic-full-name @{upstream}
>
> In all fairness, the @{upstream} syntax requires git >= 1.7.0
>
> http://www.kernel.org/pub/software/scm/git/docs/RelNotes-1.7.0.txt
>
Jeff King· Sep 23, 2010, 05:37 UTC · re: Ramana Kumar · lore

Re: get-upstream

On Thu, Sep 23, 2010 at 02:16:43PM +1000, Ramana Kumar wrote:
> Thanks Pat - that's awesome.
> Is there a git shortcut for removing the refs/remotes or refs/heads
> prefix? (I know I can just use other progs for that too)

If you use the for-each-ref solution, you can use the :short modifier, like:

  git for-each-ref --format='%(upstream:short)' `git symbolic-ref HEAD`
-Peff
Pat Notz· Sep 23, 2010, 14:28 UTC · re: Jeff King · lore

Re: get-upstream

On Wed, Sep 22, 2010 at 11:37 PM, Jeff King <peff@peff.net> wrote:
Show 13 quoted lines
> On Thu, Sep 23, 2010 at 02:16:43PM +1000, Ramana Kumar wrote:
>
>> Thanks Pat - that's awesome.
>> Is there a git shortcut for removing the refs/remotes or refs/heads
>> prefix? (I know I can just use other progs for that too)
>
> If you use the for-each-ref solution, you can use the :short modifier,
> like:
>
>  git for-each-ref --format='%(upstream:short)' `git symbolic-ref HEAD`
>
> -Peff
>

That's pretty cool. Using the >=1.7.0 syntax you can also specify an alternate branch like

$ git rev-parse --symbolic-full-name other-branch@{upstream}

Also, looking at TFM I discovered that @{u} is valid shorthand for @{upstream} -- sweet.

Still, I think Peff's solution looks best and covers more versions of git.
Jeff King· Sep 23, 2010, 05:36 UTC · re: Pat Notz · lore

Re: get-upstream

On Wed, Sep 22, 2010 at 10:15:00PM -0600, Pat Notz wrote:
> On Wed, Sep 22, 2010 at 10:05 PM, Pat Notz <patnotz@gmail.com> wrote:
> > $ git rev-parse --symbolic-full-name @{upstream}
> 
> In all fairness, the @{upstream} syntax requires git >= 1.7.0
You can also use:
  git for-each-ref --format='%(upstream)' `git symbolic-ref HEAD`

which has worked since v1.6.3. It also has the advantage that you can ask for the upstream of something besides the HEAD.

-Peff

← back to recent threads