Skip to content

Instantly share code, notes, and snippets.

@mkropat
Last active May 22, 2019 16:12
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 mkropat/168309e41e3551f7aaa86e2fe7913ed5 to your computer and use it in GitHub Desktop.
Save mkropat/168309e41e3551f7aaa86e2fe7913ed5 to your computer and use it in GitHub Desktop.
#!/bin/sh
SUBDIRECTORY_OK=1
OPTIONS_SPEC='git go [options] [branch]
--
b,branch create and checkout a new branch
'
. "$(git --exec-path)/git-sh-setup"
checkout_opts=
while test "$#" != 0; do
case "$1" in
-b|--branch)
checkout_opts="-b"
;;
--)
shift
break
;;
*)
usage
;;
esac
shift
done
does_branch_exist() {
git rev-parse "$1" >/dev/null 2>&1
}
has_upstream() {
git rev-parse "$1@{upstream}" >/dev/null 2>&1
}
if [ -z "$1" ]; then
git pull --ff-only
exit
fi
upstream=origin
branch="$1"
if [ "$#" -gt 1 ]; then
upstream="$1"
branch="$2"
fi
git fetch "$upstream" "$branch:$branch" 2>/dev/null &&
if ! has_upstream "$branch"; then
git branch --set-upstream-to="$upstream/$branch" "$branch"
fi
git checkout $checkout_opts "$branch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment