Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"
@davetcoleman

This comment has been minimized.

Copy link

@davetcoleman davetcoleman commented May 26, 2014

This is exactly what I have been looking for, for months. Thanks!!

@chuckbjones

This comment has been minimized.

Copy link

@chuckbjones chuckbjones commented Jul 3, 2014

Modified version that does the exact opposite, changing ssh urls to https:
https://gist.github.com/chuckbjones/9dc6634fe52e56ba45ac

@brettalton

This comment has been minimized.

Copy link

@brettalton brettalton commented Jan 14, 2015

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!

@chicks-net

This comment has been minimized.

Copy link

@chicks-net chicks-net commented Feb 3, 2015

Thanks for saving me from writing this. 🍰

@amcolash

This comment has been minimized.

Copy link

@amcolash amcolash commented May 4, 2015

So great! Thanks much :)

@Ardakilic

This comment has been minimized.

Copy link

@Ardakilic Ardakilic commented Jun 9, 2015

There's an official help guide for this if anyone's interested:

https://help.github.com/articles/changing-a-remote-s-url/

@bhagyas

This comment has been minimized.

Copy link

@bhagyas bhagyas commented Sep 21, 2015

Added a script for BitBucket conversion.

https://gist.github.com/bhagyas/7c1144e295072340b1ae

@BrendagaoW

This comment has been minimized.

Copy link

@BrendagaoW BrendagaoW commented Nov 2, 2015

Thanks a lot @brettalton

@saneshark

This comment has been minimized.

Copy link

@saneshark saneshark commented Nov 9, 2015

Would be nice if it accepted an argument for ^origin or could change all remotes that matched some regexp. Using xargs -- i might fork this and create.

@hvpandya

This comment has been minimized.

Copy link

@hvpandya hvpandya commented Nov 10, 2015

That solved the issue real quick, thanks!

@emilyhoughkovacs

This comment has been minimized.

Copy link

@emilyhoughkovacs emilyhoughkovacs commented Nov 11, 2015

@Ardakilic ty. learned something new about git today :)

@michaelsilver

This comment has been minimized.

Copy link

@michaelsilver michaelsilver commented Dec 21, 2015

Modified it to work for both origin and upstream for those of us with forks too.

https://gist.github.com/michaelsilver/6aa07e35a31f1f6b2e55

@dgem

This comment has been minimized.

Copy link

@dgem dgem commented Dec 23, 2015

@m14t Nice work, had to do a minor tweak since my repo URLs didn't end in '.git' otherwise works like a charm 👍

@icyflame

This comment has been minimized.

Copy link

@icyflame icyflame commented Apr 6, 2016

Modified it to work for all the remote URLs in a particular git repository. It loops through the output of git remote and changes everything to SSH. (Handy if you are moving from a proxied environment to an open one.)

https://gist.github.com/icyflame/532edee5422baeabac56d111f642bd73

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 8, 2016

Works beautifully - thanks @brettalton!

@mcouthon

This comment has been minimized.

Copy link

@mcouthon mcouthon commented May 25, 2016

Excellent job. Worked flawlessly!

@RichardLitt

This comment has been minimized.

Copy link

@RichardLitt RichardLitt commented Sep 23, 2016

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

@kajuwise

This comment has been minimized.

Copy link

@kajuwise kajuwise commented Jan 10, 2017

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

@gitfrage

This comment has been minimized.

Copy link

@gitfrage gitfrage commented Apr 13, 2017

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

@phinze

This comment has been minimized.

Copy link

@phinze phinze commented May 6, 2017

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

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

This comment has been minimized.

Copy link

@roy-bukapeta roy-bukapeta commented May 17, 2017

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

Best way to convert https to use ssh

@simonherbert

This comment has been minimized.

Copy link

@simonherbert simonherbert commented Jan 27, 2018

Thanks! Works perfect without any complicated stuff :-)

@seanlerner

This comment has been minimized.

Copy link

@seanlerner seanlerner commented Mar 15, 2018

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

@Dam0cles

This comment has been minimized.

Copy link

@Dam0cles Dam0cles commented Mar 21, 2018

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

This comment has been minimized.

Copy link

@wjholden wjholden commented Sep 12, 2018

Thank you!

@cdsandoval

This comment has been minimized.

Copy link

@cdsandoval cdsandoval commented Mar 2, 2019

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 !!

@srghma

This comment has been minimized.

Copy link

@srghma srghma commented May 31, 2019

@spacemudd

This comment has been minimized.

Copy link

@spacemudd spacemudd commented Jun 9, 2019

This worked beautifully. Thank you.

@karansapolia

This comment has been minimized.

Copy link

@karansapolia karansapolia commented Sep 19, 2019

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

This comment has been minimized.

Copy link

@gerryfrank10 gerryfrank10 commented Mar 11, 2020

Nice Work, This saved me from a lot of work

@shivams

This comment has been minimized.

Copy link

@shivams shivams commented Jul 25, 2020

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

This comment has been minimized.

Copy link

@prabinb prabinb commented Jul 31, 2020

Thanks @m14t. You saved my day!

@ptanmay143

This comment has been minimized.

Copy link

@ptanmay143 ptanmay143 commented Sep 27, 2020

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

This comment has been minimized.

Copy link

@shivams shivams commented Sep 28, 2020

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

@AbdElraoufSabri

This comment has been minimized.

Copy link

@AbdElraoufSabri AbdElraoufSabri commented Dec 21, 2020

One line

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

From my fork: Here

@nurShom

This comment has been minimized.

Copy link

@nurShom nurShom commented Jan 13, 2021

Awesome! Made my life so much easier.

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