Skip to content

Instantly share code, notes, and snippets.

@aduth
Created March 8, 2018 03:03
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 aduth/868f08b958312243ba68cf828b90b9f0 to your computer and use it in GitHub Desktop.
Save aduth/868f08b958312243ba68cf828b90b9f0 to your computer and use it in GitHub Desktop.
Checkout from GitHub remote via `user:branch` syntax
#!/bin/bash
# https://stackoverflow.com/a/26843249/995445
IFS=':' read -ra parts <<< "$1"
if [ ${#parts[@]} == 1 ]; then
branch=${parts[0]}
else
remote=${parts[0]}
branch=${parts[1]}
fi
# https://stackoverflow.com/a/26843249/995445
if [[ -n $remote ]] && ! git config remote.${remote}.url > /dev/null; then
url=$( git config remote.origin.url | sed -e "s#github\.com/[^/]*#github.com/"$remote"#g" )
git remote add $remote $url
git fetch $remote
fi
git checkout $branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment