Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

nareshganesan commented Jan 11, 2017

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

@jay-hankins

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.