Skip to content

Instantly share code, notes, and snippets.

@jasisk
Created June 3, 2015 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasisk/5d6fbdc9d922925d842e to your computer and use it in GitHub Desktop.
Save jasisk/5d6fbdc9d922925d842e to your computer and use it in GitHub Desktop.
automatically checkout a remote's "default" branch
#!/usr/bin/env sh
# Automatically checkout a local branch that tracks whatever the remote's
# HEAD is defined as (aka, the "default" branch).
#
# Place this somewhere on your PATH and call it with:
# $ git auto-checkout [remote]
#
# Takes an optional argument specifying which remote (defaults to origin).
# Note that if the branch already exists, it will be reset to the HEAD
# of [remote]/[branch].
REMOTE=${1:-origin}
git fetch $REMOTE
[ $? -eq 0 ] || exit 1
git remote set-head $REMOTE -a
BRANCH=`git symbolic-ref --short refs/remotes/$REMOTE/HEAD | cut -d/ -f2-`
git checkout -B $BRANCH -t $REMOTE/$BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment