I had to change a bunch of Git remotes from GitHub SSH URLs (e.g. git@github.com:salcode/salcode-zsh.git) to GitHub https URLs (e.g. https://github.com/salcode/salcode-zsh.git) on a bunch of different repos. This is how I made it easier for myself on my Mac.
Note: The following is Mac specific – this is not a cross-platform solution.
Quick and Dirty
You can run
git remote -v
to see your remotes. e.g.
$ git remote -v
origin git@github.com:salcode/salcode-zsh.git (fetch)
origin git@github.com:salcode/salcode-zsh.git (push)
And then run the following to update them to https.
for REMOTE in $(git remote); do
echo $REMOTE
before_url=$(git remote get-url $REMOTE)
echo "BEFORE: ${before_url}"
if [[ ! $before_url =~ ^git@github.com: ]]; then
echo "Skipping remote $remote because it is not a valid GitHub SSH URL"
echo "---------\n"
continue
fi
git remote set-url $REMOTE https://github.com/$(git remote get-url $REMOTE | sed 's/https:\/\/github.com\///' | sed 's/git@github.com://')
echo "AFTER: $(git remote get-url $REMOTE)"
echo "---------\n"
done
This is based on this StackOverflow post How to change a connection to GitHub from SSH to HTTPS?
Leveraging GitHub CLI
Because I expect I’ll need to do this again some day (or perhaps do the reverse and convert from https to SSH), I’ve put together a quick GitHub CLI extension (see salcode/gh-remote-convert).
If you have gh (the GitHub CLI) installed, you can run
gh extension install salcode/gh-remote-convert && gh remote-convert https --all
If you decide to go this route, you can optionally remove the extension from your install when you’re done by running
gh extension remove remote-convert
Leave a Reply