Today I learned

Force git to use SSH on GitHub

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.

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

How it works

When you clone an HTTPS URL, it now gets rewritten as an SSH URL.

git clone https://github.com/rstacruz/vimfiles.git

This URL will get rewritten to access via SSH instead of HTTPS. It will instead be fetching from [email protected]_ via SSH.

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! Besides, SSH keys are often easier to manage than API tokens.

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

You have just read Force git to use SSH on GitHub, written on June 09, 2018. This is Today I Learned, a collection of random tidbits I've learned through my day-to-day web development work. I'm Rico Sta. Cruz, @rstacruz on GitHub (and Twitter!).

← More articles