2

In order to write a specific script, I need to check is the current branch has a tracked remote and what is the name of this remote. Is there a way to get only the name of the branch tacked by the current one, without having to parse git remote show origin output?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Mac Moris
  • 21
  • 1
  • 1
    possible duplicate of [git command to emit name of remote tracking branch](http://stackoverflow.com/questions/3763039/git-command-to-emit-name-of-remote-tracking-branch) – CharlesB Nov 10 '11 at 08:57

2 Answers2

3

This should work:

git rev-parse --symbolic-full-name @{u}
psyho
  • 7,142
  • 5
  • 23
  • 24
1

Since 28fb8438 (Introduce @{upstream} notation, 2009-09-10) included in Git 1.7+, you can use the HEAD@{upstream} (or HEAD@{u} for short) to achieve this.

For educational purposes, here's a shell script equivalent.

#!/bin/sh

cbranch=`git rev-parse --abbrev-ref HEAD`
cmerge=`git config branch.$cbranch.merge`
cremote=`git config branch.$cbranch.remote`
if [ $? -eq 0 ]; then
    echo "$cremote/`git rev-parse --abbrev-ref $cmerge`"
else
    return 1
fi
artagnon
  • 3,609
  • 3
  • 23
  • 26