Skip to content

Instantly share code, notes, and snippets.

@nareshganesan
Forked from jay-hankins/github_remote_switch.sh
Last active January 11, 2017 06:46
Show Gist options
  • Save nareshganesan/e5d6ad969e3df8e96e087c9bdfbf5b16 to your computer and use it in GitHub Desktop.
Save nareshganesan/e5d6ad969e3df8e96e087c9bdfbf5b16 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
git remote remove origin
git remote add -mt origin $NEW_URL
echo "Switched your GitHub repo to use https remote."
echo $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
git remote remove origin
git remote add -mt origin $NEW_URL
echo "Switched your GitHub repo to use ssh remote."
echo $NEW_URL
}
github_remote_switch (){
URL=$(git remote -v | tail -1 | awk '{ print $2 }')
if [[ $URL == https* ]]
then
github_https_to_ssh $URL
else
github_ssh_to_https $URL
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment