Skip to content

Instantly share code, notes, and snippets.

@yvs2014
Forked from FigBug/https_to_ssh
Last active June 19, 2023 21:37
Show Gist options
  • Save yvs2014/7824f9431b1b69664c0e67c43a1909ff to your computer and use it in GitHub Desktop.
Save yvs2014/7824f9431b1b69664c0e67c43a1909ff to your computer and use it in GitHub Desktop.
Convert git repo from https to ssh
#/bin/bash
#-- Script to automate https://help.github.com/articles/why-is-git-always-asking-for-my-password
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."
exit
fi
BRE='https://((gist\.)*github\.com)/([^/]*)/(.*(\.git|$))'
HOST=`echo $REPO_URL | sed -Ene"s#$BRE#\1#p"`
if [ -z "$HOST" ]; then
echo "-- ERROR: Could not identify GitHub Host."
exit
fi
GIST=`echo $REPO_URL | sed -Ene"s#$BRE#\2#p"`
USER=`echo $REPO_URL | sed -Ene"s#$BRE#\3#p"`
if [ -z "$USER" ]; then
echo "-- ERROR: Could not identify User."
exit
fi
REPO=`echo $REPO_URL | sed -Ene"s#$BRE#\4#p"`
if [ -z "$REPO" ]; then
echo "-- ERROR: Could not identify Repo."
exit
fi
[ -n "$GIST" ] && USER=''
NEW_URL="git@$HOST:$USER/$REPO"
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment