Skip to content

Instantly share code, notes, and snippets.

@m14t
Created July 5, 2012 21:57
Show Gist options
  • Select an option

  • Save m14t/3056747 to your computer and use it in GitHub Desktop.

Select an option

Save m14t/3056747 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/([^/]*)/(.*).git#\2#p'`
if [ -z "$REPO" ]; then
echo "-- ERROR: Could not identify Repo."
exit
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"
@mcouthon

Copy link
Copy Markdown

Excellent job. Worked flawlessly!

@RichardLitt

Copy link
Copy Markdown

I made this into an npm module. Not perfect, but works. https://github.com/RichardLitt/github-origin-https-to-ssh

@kajuwise

kajuwise commented Jan 10, 2017

Copy link
Copy Markdown

Useful script for lazy people!
Used with:
echo "Navigating to $1"
cd "$1"
to specify repo location via argument.

@gitfrage

Copy link
Copy Markdown

or just edit [remote "origin"] section in .git/config

@phinze

phinze commented May 6, 2017

Copy link
Copy Markdown

Hi folks! FYI git supports this as a configurable nowadays:

git config --global url."git@github.com:".insteadOf "https://github.com/"

@roy-bukapeta

Copy link
Copy Markdown

git config --global url."git@github.com:".insteadOf "https://github.com/"

Best way to convert https to use ssh

@simonherbert

Copy link
Copy Markdown

Thanks! Works perfect without any complicated stuff :-)

@seanlerner

Copy link
Copy Markdown

thx @phinze & @roy-bukapeta
your solution worked well for me.

@damokuro

Copy link
Copy Markdown

fixed it for repository names that don't end in .git:
https://gist.github.com/Dam0cles/51b0b3e711bf50499f0e23415b6a6e46
Also, (.*).git in sed with an unescaped . is dangerous as it could match https://github.com/User/Legit as "L" for this kind of repository.

@wjholden

Copy link
Copy Markdown

Thank you!

@cdsandoval

Copy link
Copy Markdown

To note, @m14t is simply running one command here, but is gathering useful information from the 30 lines above. If you know the SSH URL, you can simple run,

git remote set-url origin git@github.com:username/repo-name-here.git

Where username is the username of the repo owner and repo-name-here is the name of that user's repository.

The URL can be found in the repositories homepage in this box,

SSH URL

I hope that helps!

Thank you !!

@spacemudd

Copy link
Copy Markdown

This worked beautifully. Thank you.

@karansapolia

Copy link
Copy Markdown

To note, @m14t is simply running one command here, but is gathering useful information from the 30 lines above. If you know the SSH URL, you can simple run,

git remote set-url origin git@github.com:username/repo-name-here.git

Where username is the username of the repo owner and repo-name-here is the name of that user's repository.

The URL can be found in the repositories homepage in this box,

SSH URL

I hope that helps!

Hey thanks! 😄

@gerryfrank10

Copy link
Copy Markdown

Nice Work, This saved me from a lot of work

@shivams

shivams commented Jul 25, 2020

Copy link
Copy Markdown

This is what I use for changing my remote from HTTPS to SSH, and it works for any repository (Github, Gitlab, Bitbucket or private Git server):

#The if condition is for checking your remote HTTPS URL has `.git` at the end or not
if git config --get remote.origin.url | grep -P '\.git$' >/dev/null; then 
    newURL=`git config --get remote.origin.url | sed -r 's#(http.*://)([^/]+)/(.+)$#git@\2:\3#g'`
else
    newURL=`git config --get remote.origin.url | sed -r 's#(http.*://)([^/]+)/(.+)$#git@\2:\3.git#g'`
fi;

echo "Does this new url look fine? (y/n) : " $newURL
read response
if [[ "$response" == "y" ]]; then 
    git remote set-url origin $newURL; 
    echo "Git remote updated."; 
else 
    echo "Git remote unchanged."; 
fi;

@prabinB

prabinB commented Jul 31, 2020

Copy link
Copy Markdown

Thanks @m14t. You saved my day!

@ptanmay143

ptanmay143 commented Sep 27, 2020

Copy link
Copy Markdown

This is what I use for changing my remote from HTTPS to SSH, and it works for any repository (Github, Gitlab, Bitbucket or private Git server):

#The if condition is for checking your remote HTTPS URL has `.git` at the end or not
if git config --get remote.origin.url | grep -P '\.git$' >/dev/null; then 
    newURL=`git config --get remote.origin.url | sed -r 's#(http.*://)([^/]+)/(.+)$#git@\2:\3#g'`
else
    newURL=`git config --get remote.origin.url | sed -r 's#(http.*://)([^/]+)/(.+)$#git@\2:\3.git#g'`
fi;

echo "Does this new url look fine? (y/n) : " $newURL
read response
if [[ "$response" == "y" ]]; then 
    git remote set-url origin $newURL; 
    echo "Git remote updated."; 
else 
    echo "Git remote unchanged."; 
fi;

Fixed an error on line 5. Unexpected ; after else.

@shivams

shivams commented Sep 28, 2020

Copy link
Copy Markdown

@ptanmay143 Thanks for the fix for the typo bug. I've updated my comment as well.

@abd3lraouf

Copy link
Copy Markdown

One line

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

From my fork: Here

@nurshom

nurshom commented Jan 13, 2021

Copy link
Copy Markdown

Awesome! Made my life so much easier.

@moneer-muntazah

Copy link
Copy Markdown

Hi folks! FYI git supports this as a configurable nowadays:

git config --global url."git@github.com:".insteadOf "https://github.com/"

Thank you so much

@skahvedzic-ca

Copy link
Copy Markdown

Nice work! Thanks!

@alexandre-hanriot

Copy link
Copy Markdown

To note, @m14t is simply running one command here, but is gathering useful information from the 30 lines above. If you know the SSH URL, you can simple run,

git remote set-url origin git@github.com:username/repo-name-here.git

Where username is the username of the repo owner and repo-name-here is the name of that user's repository.

The URL can be found in the repositories homepage in this box,

SSH URL

I hope that helps!

Thanks for the help !

@roeniss

roeniss commented Aug 14, 2021

Copy link
Copy Markdown

Thank you so much guys!

@ForceGT

ForceGT commented Sep 18, 2021

Copy link
Copy Markdown

Point to be noted is that if you use Xcode with Swift Package Manager then this is going to cause issues for you as Xcode by default doesn't respect this , and continues to use its own way of doing things. It is a really irritating bug. You can check this SO question for more details

https://stackoverflow.com/questions/58165880/xcode-11-spm-authentication-failed-because-no-credentials-provided

@siduck

siduck commented Jan 15, 2022

Copy link
Copy Markdown

@brettalton thanks for the one liner!

@CodeIter

CodeIter commented Apr 9, 2022

Copy link
Copy Markdown

Hi, in case someone need to get git remote url as https, whatever your remote Git repository settings are.
more at https://gist.github.com/CodeIter/be215cfef4ec1af5e1a10c4563944869

[ -z "${GITHOST}" ] && GITHOST="github.com"
[ -z "${GITBRANCH}" ] && GITBRANCH="origin"
git remote -v \
| grep "${GITBRANCH}" | grep fetch | head -n 1 \
| sed -re 's~\s+\(fetch\)$~~' \
      -re 's~([^/ :]+/[^/ :]+)$~#\1~;s~^[^#]+#+~~' \
      -re 's~(github\.com)~\1/~' \
      -re '/^https?:\/\//!s~^~https://'"${GITHOST}"'/~'

@theowenyoung

Copy link
Copy Markdown

If your https url include user:password, like https://user:token@github.com/username/reponame, then you should use this:

REPO_URL=$(git remote -v | grep -m1 '^origin' | sed -Ene's#.*(https://[^[:space:]]*).*#\1#p')

echo "$REPO_URL"
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/([^/]*)/(.*).git#\2#p')
if [ -z "$REPO" ]; then
    echo "-- ERROR:  Could not identify Repo."
    exit
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"
eval "$CHANGE_CMD"

echo "Success"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment