There are multiple ways to set your repository remote or clone a repository. You can do it through
git (special protocol just for git). You can pair the
git protocol to the previous ones,
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 email@example.com:facebook/react.git or add a remote like
git remote add origin firstname.lastname@example.org: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.
And also, sometimes some programs use
https and you want to use
That’s why I’m writing this, we can provide a set of aliases that replace the https protocol with the ssh protocol automatically.
Create or edit the file
.gitconfig in your user’s home directory and put these lines in.
1# Enforce SSH2 [url "email@example.com:"]3 insteadOf = https://github.com/4 [url "firstname.lastname@example.org:"]5 insteadOf = https://gitlab.com/6 [url "email@example.com:"]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.