When working with repositories on GitHub, Bitbucket, GitLab, and other platforms, it is common to find two ways to clone a repository. The first involves using the HTTP/HTTPS protocol, and the second involves using the SSH protocol. Today, we will see how to download a repo and perform GIT operations via SSH with an RSA key.
The first thing you need to do is create your own RSA key. To do this, I recommend visiting the article What is an RSA key, how to create it, and how to use it. The next step is to log into the VCS platform and add the public part of the rsa key. This last part depends a lot on the system you are using.
You go to Settings > SSH and GPG keys and click on New SSH Key.
You go to Personal Settings > SSH keys and click on Add key.
Once you have completed this step, you can clone a repository via SSH like this.
git clone email@example.com:pleets/php-http-clients.git
Running commands with a specific RSA key
Sometimes you need to download a private repo with an rsa key different from the usual one, which has the name id_rsa. It turns out that by default, this key is configured to be used without asking for it, and there is no way to send a parameter in console such as
-i key.pem so that GIT commands use that key. If you already have an id_rsa key, the repo cloning will use it and not another, as well as any actions that interact with the server such as fetch, pull, and push. One solution to this problem is to use a git configuration that allows you to set this key previously like this.
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example"
However, for this command to work, you must have previously downloaded the repository via HTTP/HTTPS. Once you have done this and are in the repo path, you can execute this command, and after that, any command with interaction with the server will use the specified key.