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字符串对于不同的用户是相同的,这不是问题。)
即使用户和主机是相同的,在~/.ssh/config中仍然可以区分它们。例如,如果你的配置是这样的:
Host gitolite-as-alice
HostName git.company.com
User git
IdentityFile /home/whoever/.ssh/id_rsa.alice
IdentitiesOnly yes
Host gitolite-as-bob
HostName git.company.com
User git
IdentityFile /home/whoever/.ssh/id_dsa.bob
IdentitiesOnly yes
然后在URL中使用gitolite-as-alice和gitolite-as-bob代替主机名:
git remote add alice git@gitolite-as-alice:whatever.git
git remote add bob git@gitolite-as-bob:whatever.git
Note
您希望包括IdentitiesOnly yes选项,以防止使用默认id。否则,如果您也有与默认名称匹配的id文件,它们将首先被尝试,因为与其他配置选项(遵守“first in wins”)不同,IdentityFile选项附加到要尝试的身份列表中。参见:https://serverfault.com/questions/450796/how-could-i-stop-ssh-offering-a-wrong-key/450807 # 450807
注意:有一种方法可以在之前的回答中应用目录的git配置,而不仅仅是在回购级别。
在我的例子中,所有的项目都在~/work/目录下,所以我更新了我的gitconfig如下:
# ~/.gitconfig
...
[includeIf "gitdir:~/work/**"]
path = "~/work/.gitconfig"
# ~/work/.gitconfig
[user]
email = amr@work.com
name = Amr Awad
sshCommand = ssh -i ~/.ssh/work
现在~/work/目录下的所有回购操作都使用工作键~/。Ssh /work,不需要分别配置每个repo。