Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify SSH configuration for Git per-repository

Tags:

git

ssh

mercurial

In Mercurial, I can specify the command (and thus the certificate) used for SSH per-repository in .hg/hgrc, for example:

[ui]
...
ssh = "C:\path\to\ssh.exe" -i "C:\Development\Identities\identity1.id_rsa"

Most instructions I can find on configuring Git seem to use the bundled ssh.exe to manage the certificates, for example GitHub's guide for Windows. There is a way of specifying a script for Git to use for SSL (here under "No supported authentication methods available") via an environment variable, but that per user and not granular enough for me.

How can I specify a per-repository SSH command line in Git?

like image 891
Rebecca Scott Avatar asked Jun 11 '26 16:06

Rebecca Scott


1 Answers

I'm unaware of a way to make git do this. But ssh should be flexible enough to do it instead.

First note that you can specify more than one identity file. It will try multiple identities in turn, until one is accepted.

Second, under unix, if you want only certain identities to be tried for certain hosts, you can do so by modifying ~/.ssh/config to have stanzas such as:

Host <hostname>
     IdentitiesOnly yes
     IdentityFile "C:\Development\Identities\identity1.id_rsa"

Though I don't know if that will work for your bundled windows ssh client, or where the config file goes, if it does.

EDIT: if you want to use two different identities on the same host, that can be done too:

Host <alias1>
     HostName <realhostname>
     IdentiesOnly yes
     IdentityFile identity1


Host <alias2>
     HostName <realhostname>
     IdentiesOnly yes
     IdentityFile identity2

and use and as the hosts in git (there is substantial support to treat one host as another, or do even stronger rewriting with config options such as url.<base>.insteadOf.

like image 147
wnoise Avatar answered Jun 14 '26 08:06

wnoise