We may utilize different keys for different projects or accounts. When we pull data from a Git repository through an SSH connection, by default the Git tool will follow the default SSH configuration for selecting the key used which is located in ~/.ssh/id_rsa
. We can also set a custom SSH configuration located in the ~/.ssh/config
file that will be followed by the Git tool too as explained in my other post.
For setting the private key locally or per session, there are other options. First, we can utilize an environment variable that will be read by the Git tool for selecting the correct SSH command which is GIT_SSH_COMMAND
. The usage is as follows.
GIT_SSH_COMMAND="ssh -i ~/.ssh/your_id_rsa -F /dev/null" git clone git@github.com:your/project.git
The -F /dev/null
parameter is used for ignoring any available SSH configuration in the host. This method will apply the custom SSH command during the user session or it can be permanent too by setting it in the host environment variable.
We can also make the setting applied per repository by using a Git configuration parameter as follows.
git clone -c "core.sshCommand=ssh -i ~/.ssh/your_id_rsa -F /dev/null" git@github.com:your/project.git
If the repository already exists locally, we can run the following command inside the repository.
git config core.sshCommand "ssh -i ~/.ssh/your_id_rsa -F /dev/null"
The last two commands will add a configuration parameter inside the .git/config
file of the project repository.
Comments
Post a Comment