Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 40 lines (36 sloc) 911 Bytes
#!/bin/bash
#
# Usage: git tracks [branch]
#
# Get the upstream branch of the given branch. If no branch is given, get the
# upstream branch of the currrent branch.
#
# If -d/--default is passed, outputs origin/master if there's no upstream
# branch.
#
# To set <branch>'s upstream branch, run
#
# $ git branch --set-upstream <branch> <upstream>
#
#
# Based on http://serverfault.com/a/352236/38694
if [[ "$1" == "-d" || "$1" == "--default" ]]; then
use_default=1
shift
else
use_default=0
fi
if [[ "$1" == "" ]]; then
branch=HEAD
else
branch=$1
fi
branchref=$(git rev-parse --symbolic-full-name $branch) || exit $?
remote=$(git for-each-ref --format='%(upstream:short)' $branchref)
if [[ $remote != "" ]]; then
echo $remote
elif [[ "$use_default" == 1 ]] && git rev-parse origin/master &>/dev/null; then
echo origin/master
else
echo >&2 "Warning: Could not find a upstream for $branch"
fi