Skip to content

Instantly share code, notes, and snippets.

@abd3lraouf
Forked from damokuro/fix_github_https_repo.sh
Last active March 31, 2021 09:39
Show Gist options
  • Save abd3lraouf/7fac905f1ee08e9127a7d0ff4f08abb6 to your computer and use it in GitHub Desktop.
Save abd3lraouf/7fac905f1ee08e9127a7d0ff4f08abb6 to your computer and use it in GitHub Desktop.
Convert HTTPS github clones to use SSH

Introduction

converts git remote from https to ssh using sed

One line

wget https://bit.ly/https_ssh_manoo -O- | bash

Features:

  1. Downlaod
  2. Execute
  3. Self delete
#!/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="[email protected]:$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