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

commented May 26, 2014

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

@chuckbjones

This comment has been minimized.

Copy link

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

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

commented Feb 3, 2015

Thanks for saving me from writing this. 🍰

@amcolash

This comment has been minimized.

Copy link

commented May 4, 2015

So great! Thanks much :)

@Ardakilic

This comment has been minimized.

Copy link

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

commented Sep 21, 2015

Added a script for BitBucket conversion.

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

@BrendagaoW

This comment has been minimized.

Copy link

commented Nov 2, 2015

Thanks a lot @brettalton

@saneshark

This comment has been minimized.

Copy link

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

commented Nov 10, 2015

That solved the issue real quick, thanks!

@emilyhoughkovacs

This comment has been minimized.

Copy link

commented Nov 11, 2015

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

@michaelsilver

This comment has been minimized.

Copy link

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

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

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

@denise72214

This comment has been minimized.

Copy link

commented Apr 8, 2016

Works beautifully - thanks @brettalton!

@mcouthon

This comment has been minimized.

Copy link

commented May 25, 2016

Excellent job. Worked flawlessly!

@RichardLitt

This comment has been minimized.

Copy link

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

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

commented Apr 13, 2017

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

@phinze

This comment has been minimized.

Copy link

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

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

commented Jan 27, 2018

Thanks! Works perfect without any complicated stuff :-)

@seanlerner

This comment has been minimized.

Copy link

commented Mar 15, 2018

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

@Dam0cles

This comment has been minimized.

Copy link

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

commented Sep 12, 2018

Thank you!

@cdsandoval

This comment has been minimized.

Copy link

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

commented May 31, 2019

@spacemudd

This comment has been minimized.

Copy link

commented Jun 9, 2019

This worked beautifully. Thank you.

@karansapolia

This comment has been minimized.

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.