Forked from jay-hankins/github_remote_switch.sh
Last active
January 11, 2017 06:46
-
-
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.
This file contains 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 | |
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