git – make an existing git branch track a remote branch

8 Aug 2011

A script to make an existing git branch track a remote branch. For example when you’ve cloned from somewhere else and now want to track your normal remote.

% cat ~/bin/gittrack
#!/bin/bash
# vim: ai ts=4 sts=4 et sw=4 ft=sh

current_branch=$(git symbolic-ref -q HEAD)
current_branch=${current_branch##refs/heads/}
current_branch=${current_branch:-HEAD}

if [ $current_branch = 'HEAD' ] ; then
    echo
    echo "On a detached head. Exiting..."
    exit 1
fi

remote=${1:-origin}

git branch --set-upstream $current_branch $remote/$current_branch

Of course your global gitconfig should also have autosetupmerge (below), but this script handles situations where you want to setup/change the tracking branch.

[branch]
        autosetupmerge = true
comments powered by Disqus

  « Previous: Next: »