-
-
Save matheusavi/b2137da89bf1b7720925bfa195be8866 to your computer and use it in GitHub Desktop.
Convert HTTPS github clones from all subdirectories to use SSH
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
| #/bin/bash | |
| #Just put this script in the root folder and run it | |
| for d in *; do | |
| if [ -d "$d" ]; then | |
| echo $d | |
| cd $d | |
| REPO_URL=`git remote -v | grep -m1 '^origin' | sed -Ene's#.*(https://[^[:space:]]*).*#\1#p'` | |
| if [ -z "$REPO_URL" ]; then | |
| echo "-- ERROR: Could not identify Repo url." | |
| echo " It is possible this repo is already using SSH instead of HTTPS." | |
| cd .. | |
| continue | |
| fi | |
| REPO_URL="${REPO_URL/.git/}" | |
| echo $REPO_URL | |
| USER=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*)#\1#p'` | |
| if [ -z "$USER" ]; then | |
| echo "-- ERROR: Could not identify User." | |
| cd .. | |
| continue | |
| fi | |
| echo $USER | |
| REPO=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*)#\2#p'` | |
| if [ -z "$REPO" ]; then | |
| echo "-- ERROR: Could not identify Repo." | |
| cd .. | |
| continue | |
| fi | |
| NEW_URL="[email protected]:$USER/$REPO.git" | |
| echo "Changing repo url from " | |
| echo " '$REPO_URL'" | |
| echo " to " | |
| echo " '$NEW_URL'" | |
| echo "" | |
| CHANGE_CMD="git remote set-url origin $NEW_URL" | |
| `$CHANGE_CMD` | |
| echo "Success" | |
| cd ../ | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment