Force git to use SSH on GitHub

I can use HTTPS and SSH URL's for GitHub repos, thanks to insteadOf

Written by Rico Sta. Cruz
(@rstacruz) · 9 June 2018

This forces GitHub to always use SSH, even for HTTPS URL’s! This way, you can use https:// GitHub URL’s and they will still use your SSH key.

Terminal window
git config --global url."[email protected]:".insteadOf "https://github.com/"

How it works

When cloning an HTTPS URL, it now gets rewritten as an SSH URL. It will instead be fetching from [email protected] via SSH.

Terminal
git clone https://github.com/rstacruz/vimfiles.git
# ...actually clones [email protected]:rstacruz/vimfiles.git

The inverse: Use HTTPS instead of SSH

Alternatively, this forces GitHub to always use HTTPS. Great for macOS, where you can use git-credential-osxkeychain to store your GitHub token. This will not rewrite [email protected]:user/repo.git URL’s, though, so I don’t really recommend it!

Terminal window
git config --global url."https://github".insteadOf git://github

References

Hat tip to these people!

Written by Rico Sta. Cruz

I am a web developer helping make the world a better place through JavaScript, Ruby, and UI design. I write articles like these often. If you'd like to stay in touch, subscribe to my list.

Comments

More articles

← More articles