Skip to content

Instantly share code, notes, and snippets.

@jdpaton
Last active December 21, 2015 23:38
Show Gist options
  • Save jdpaton/6383313 to your computer and use it in GitHub Desktop.
Save jdpaton/6383313 to your computer and use it in GitHub Desktop.
Clones or updates and checks out the appropriate branch while checking for existence of either.
#!/bin/bash
TARGET_DIR=${1:-"./new-repo"}
BRANCH=${2:-"master"}
REPO=${3:-"git://repo/name.git"}
if [ -e "${TARGET_DIR}" ]
then
pushd $TARGET_DIR
git fetch
exists=$(git show-branch $BRANCH > /dev/null 2>&1; echo $?)
if [ "$exists" == "0" ]
then
git checkout $BRANCH
else
git checkout -b $BRANCH origin/$BRANCH
fi
git reset --hard origin/$BRANCH
popd
else
git clone $REPO $TARGET_DIR
fi
pushd $TARGET_DIR
git show
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment