Besides HTTPS, we can make a connection to a Git repository using SSH. For authentication, we should store our public key on the remote host, and set the remote Git URL on our host to the correct address for SSH connection. For example:
git remote set-url myremote ssh://git@myremotegit.com/myrepo.git
Common SSH client tools provide a specific parameter to set which private key should be used for authentication. For example, the OpenSSH client provides -i
parameter to specify the location of the private key that will be used. Meanwhile, common Git client tools may not provide it. When we run a Git command, the tool will look for a private key stored in the default directory which is ~/.ssh/id_rsa
. We can resolve this issue by setting up a configuration file that will be stored in ~/.ssh/config
. For instance, this is a sample of a configuration file.
Host myremotegit
HostName myremotegit.com
User git
IdentityFile C:\\keys1\\id_rsa
IdentitiesOnly yes
Host bitbucket-com
HostName bitbucket.com
User git
IdentityFile C:\\keys2\\id_rsa
IdentitiesOnly yes
We set the Host
parameter into a custom name while HostName
is the actual address of the host. Then, we can set the remote Git URL using the custom name. This method will make any SSH client including Git client read the ~/.ssh/config
file before they can establish an SSH connection. For example:
git remote set-url myremote ssh://git@myremotegit/myrepo.git
Comments
Post a Comment