Last active
June 2, 2017 22:41
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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="[email protected]:" | |
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 | |
} | |
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
Thanks, really useful. ๐ I've forked your gist and just made a small change.