Since happily subverting Subversion and switching to Git, I’ve been looking for this tip: how to set up auto-tracking of a local branch that I pushed remotely (e.g. so that I can git pull
without naming the remote branch). It is simple if you create your local branch from an existing remote branch (git branch --track foo origin/foo
) and I assumed there must be a similar flag if I pushed my local branch to a remote (without creating a new branch or creating the remote first).
Well, Jamis Buck had the same question and Sam Stephenson provided the answer, reproduced below. (Who says Twitter is a waste of time?) After adding this script and setting up the git alias, you can accomplish it via ‘git push origin foo && git track
’.
#!/bin/sh
# git-track should be added to your path.
# Sets up auto-tracking of a remote branch with same base name.
# Can set up "git track" so it feels built-in:
# git config --global --add alias.track '!git-track'
#
branch=$(git branch 2>/dev/null | grep ^\*)
[ x$1 != x ] && tracking=$1 || tracking=${branch/* /}
git config branch.$tracking.remote origin
git config branch.$tracking.merge refs/heads/$tracking
echo "tracking origin/$tracking"
And if you truly hate unnecessary typing install the slick git auto-completion script and bask in the saved keystrokes.