Navigate back to the homepage

Automatically make git clone use ssh instead of https

Jaime Martínez Rincón
June 27th, 2020 · 1 min read

The reason

There are multiple ways to set your repository remote or clone a repository. You can do it through https, ssh and git (special protocol just for git). You can pair the git protocol to the previous ones, https or ssh as the git protocol by itself does not support authentication. For more information about the protocols Git supports in The protocols - The Git SCM book

I think I don’t need the git protocol and that I’m better off using just the ssh protocol for everything.

Why? For one, ssh is more secure and it’s faster. On the other hand, https is easier to go through firewalls, but if you have 2FA enabled in GitHub, for example, you will have to create an access token and use that as a password. I have to say that the credentials to use both protocols can be saved and remembered automatically, if you use ssh you can use an ssh agent and if you use https you have to use a credential helper.

That being said, I had a better experience using ssh so I will keep using that, but GitHub recommends users to use https as it’s easier and more straightforward to use.

So what? I can just clone like git clone git@github.com:facebook/react.git or add a remote like git remote add origin git@github.com:facebook/react.git.

The problem is that not everyone uses the ssh protocol, I sometimes just copy the URL of the repository from the browser’s omnibox and try to clone that, that will go through https, obviously.

Yeah… I know you can use the clone button of git and get a URL that uses the git protocol, but sometimes I’m too lazy to do that.

GitHub's repository clone dialog

And also, sometimes some programs use https and you want to use ssh instead.

That’s why I’m writing this, we can provide a set of aliases that replace the https protocol with the ssh protocol automatically.

The trick

Create or edit the file .gitconfig in your user’s home directory and put these lines in.

1# Enforce SSH
2 [url "git@github.com:"]
3 insteadOf = https://github.com/
4 [url "git@gitlab.com:"]
5 insteadOf = https://gitlab.com/
6 [url "git@bitbucket.org:"]
7 insteadOf = https://bitbucket.org/

That will make sure all https URLs are converted to ssh ones automatically. You can also do it in reverse in case you need a special case for some repository.

More articles from Jaime Martínez Rincón

Fix Visual Studio Code freezing when deleting

If you have ever used an Electron-based application that deletes files or directories by moving them to the trash bin under KDE or other Qt…

May 16th, 2020 · 1 min read

Fix missing libtinfo.so.5 library in Arch Linux

If you try to use clang-format in Arch Linux you might find yourself with an error saying that the library libtinfo.so.5 couldn't be…

May 16th, 2020 · 1 min read
© 2020 Jaime Martínez Rincón
Link to $https://github.com/jamezrinLink to $https://twitter.com/jamezrinLink to $https://stackoverflow.com/users/4673065/jamezrinLink to $https://www.linkedin.com/in/jamezrin/