I have the following use case: I would like to be able to push to git@git.company.com:gitolite-admin using the private key of user gitolite-admin, while I want to push to git@git.company.com:some_repo using 'my own' private key. AFAIK, I can't solve this using ~/.ssh/config, because the user name and server name are identical in both cases. As I mostly use my own private key, I have that defined in ~/.ssh/config for git@git.company.com. Does anyone know of a way to override the key that is used for a single git invocation?

(另外:gitolite根据键来区分谁在进行推送,所以就访问、所有权和审计而言,user@server字符串对于不同的用户是相同的,这不是问题。)


当前回答

使用git config配置您的存储库

git config --add --local core.sshCommand 'ssh -i <<<PATH_TO_SSH_KEY>>>'

这只适用于本地存储库。

其他回答

如果在windows上使用Git版本的ssh, ssh配置中的身份文件行如下所示

IdentityFile /c/Users/Whoever/.ssh/id_rsa.alice

其中/c代表c:

要检查,在git的bash做

cd ~/.ssh
pwd 

另一种替代方法是使用ssh-ident来管理您的ssh身份。

它会根据您当前的工作目录、ssh选项等自动加载和使用不同的密钥……这意味着您可以轻松地拥有一个工作/目录和一个私有/目录,它们在ssh中透明地使用不同的密钥和身份。

一个基于Unix的系统(Linux、BSD、Mac OS X),默认标识存储在$HOME/目录下。Ssh,分2个文件: 私钥:$HOME/.ssh/id_rsa 公钥:$HOME/.ssh/id_rsa.pub 当您使用没有选项-i的ssh时,它将使用默认的私钥对远程系统进行身份验证。

如果您有另一个想要使用的私钥,例如$HOME/。Ssh /deploy_key,您必须使用Ssh -i ~/。ssh / deploy_key……

这很烦人。您可以在$HOME/中添加以下行。bash_profile: ssh-add ~ / . ssh / deploy_key ssh-add ~ / . ssh / id_rsa

因此,每次使用ssh、git或scp(基本上也是ssh)时,都不必再使用option -i了。

您可以在$HOME/.bash_profile文件中添加任意多的键。

我在Win7上使用Git Bash。下面的方法对我很有效。

在~/处创建一个配置文件。Ssh /config或c:/users/[your_user_name]/. Ssh /config。在文件中输入:

Host your_host.com
     IdentityFile [absolute_path_to_your_.ssh]\id_rsa

我想主机必须是一个URL,而不仅仅是一个“名称”或参考您的主机。例如,

Host github.com
     IdentityFile c:/users/[user_name]/.ssh/id_rsa

也可以写入“/c/users/[user_name]/....”格式

Giordano Scalzo提供的解决方案也很棒。 https://stackoverflow.com/a/9149518/1738546

对于git来说,除了更改配置文件,它应该使用不同的SSH密钥,如下所述: https://stackoverflow.com/a/7927828/1306884 您可能还需要清除并重新加载活动的SSH标识。

在Mac上,执行以下操作:

ssh-add -D
ssh-add ~/.ssh/id_rsa_one_that_you_want_to_use_instead

使用这两个命令,并设置GIT URL以匹配ssh/config文件的Host中定义的字符串,应该允许您为不同的存储库使用不同的ssh密钥。

例如,对于主机work.github.com,在克隆存储库时使用work.github.com作为URL git@work.github.com:your/repository.git。