Skip to content

Instantly share code, notes, and snippets.

@mkropat

mkropat/git-go

Last active May 22, 2019
Embed
What would you like to do?
#!/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