Skip to content

Instantly share code, notes, and snippets.

@jay-hankins
Last active June 2, 2017 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jay-hankins/2381100f5e30d34da928407e40ce3c6a to your computer and use it in GitHub Desktop.
Save jay-hankins/2381100f5e30d34da928407e40ce3c6a to your computer and use it in GitHub Desktop.
Basic shell function to switch your GitHub repo remote URL from (https|ssh) to (ssh|https) respectively.
# Basic shell function to switch your GitHub repo
# remote URL from (https|ssh) to (ssh|https) respectively.
# Tested with zsh and zprezto on macOS 10.12
github_ssh_to_https (){
URL_PART=$("grep" "-Eo" "([A-z0-9-]+\/[A-z0-9-]+.git)" <<< $1)
# echo $URL_PART
NEW_URL="https://github.com/"
NEW_URL+=$URL_PART
# echo $NEW_URL
git remote remove origin
git remote add -mt origin $NEW_URL
}
github_https_to_ssh(){
URL_PART=$("grep" "-Eo" "([A-z0-9-]+\/[A-z0-9-]+.git)" <<< $1)
# echo $URL_PART
NEW_URL="git@github.com:"
NEW_URL+=$URL_PART
# echo $NEW_URL
git remote remove origin
git remote add -mt origin $NEW_URL
}
github_remote_switch (){
URL=$(git remote -v | tail -1 | awk '{ print $2 }')
if [[ $URL == https* ]]
then
github_https_to_ssh $URL
echo "Switched your GitHub repo to use ssh remote."
else
github_ssh_to_https $URL
echo "Switched your GitHub repo to use https remote."
fi
}
@nareshganesan
Copy link

Thanks, really useful. 👍 I've forked your gist and just made a small change.

@jay-hankins
Copy link
Author

jay-hankins commented Jan 25, 2017

Nice change, Naresh. Glad it was helpful! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment