Skip to content

Instantly share code, notes, and snippets.

@damokuro
Forked from m14t/fix_github_https_repo.sh
Created March 21, 2018 11:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save damokuro/51b0b3e711bf50499f0e23415b6a6e46 to your computer and use it in GitHub Desktop.
Save damokuro/51b0b3e711bf50499f0e23415b6a6e46 to your computer and use it in GitHub Desktop.
Convert HTTPS github clones to use 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
USER=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*)(\.git)?#\1#p'`
if [ -z "$USER" ]; then
echo "-- ERROR: Could not identify User."
exit
fi
REPO=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*)#\2#p'`
if [ -z "$REPO" ]; then
echo "-- ERROR: Could not identify Repo."
exit
fi
NEW_URL="git@github.com:$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