Skip to content

Instantly share code, notes, and snippets.

@josemarcosrf
Last active August 28, 2020 16:17
Show Gist options
  • Select an option

  • Save josemarcosrf/f0c4b42528a3c1a164b6391413986b77 to your computer and use it in GitHub Desktop.

Select an option

Save josemarcosrf/f0c4b42528a3c1a164b6391413986b77 to your computer and use it in GitHub Desktop.
Convert in batch github HTTPS repos remote url to SSH
#/bin/bash
# Script to convert HTTPS to SSH github remote URLs providing a parent folder
# containing all the repo directories (i.e.: with a .git subdirectory)
#
# This script was inspired by https://gist.github.com/m14t/3056747
directoryContainingAllRepos=$1
cd $directoryContainingAllRepos
for dir in $(find * -maxdepth 0 -type d \( ! -name . \) -print)
do
echo ""
echo "----------------------------------------------------"
cd "$directoryContainingAllRepos/$dir"
echo "Looking at directory: $(pwd)"
echo ""
if [ -d ".git" ]; then
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."
continue;
fi
USER=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*).git#\1#p'`
if [ -z "$USER" ]; then
echo "-- ERROR: Could not identify User."
continue;
fi
REPO=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*).git#\2#p'`
if [ -z "$REPO" ]; then
echo "-- ERROR: Could not identify Repo."
continue;
fi
NEW_URL="git@github.com:$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"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment