Skip to content

Instantly share code, notes, and snippets.

@matheusavi
Forked from m14t/fix_github_https_repo.sh
Last active March 24, 2021 12:19
Show Gist options
  • Save matheusavi/b2137da89bf1b7720925bfa195be8866 to your computer and use it in GitHub Desktop.
Save matheusavi/b2137da89bf1b7720925bfa195be8866 to your computer and use it in GitHub Desktop.
Convert HTTPS github clones from all subdirectories to use SSH
#/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