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?
Asked
Active
Viewed 196 times
2
-
1possible 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 Answers
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