Skip to content

Instantly share code, notes, and snippets.

@aduth
Created June 11, 2018 13:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aduth/93dbd5c528bec6451083272f39338dab to your computer and use it in GitHub Desktop.
Git Checkout GitHub Remote Branch
#!/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