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字符串对于不同的用户是相同的,这不是问题。)
我已经在github上测试了以下方法,基于阅读其他答案,它结合了一些技巧:
正确的SSH配置
git URL重写
这种方法的优点是,一旦设置好,它不需要任何额外的工作来使它正确-例如,您不需要更改远程URL或记住以不同的方式克隆东西- URL重写使它全部工作。
~ / . ssh /配置
# Personal GitHub
Host github.com
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/github_id_rsa
# Work GitHub
Host github-work
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/work_github_id_rsa
Host *
IdentitiesOnly yes
~ / . gitconfig
[user]
name = My Name
email = personal@personal.email
[includeIf "gitdir:~/dev/work/"]
path = ~/dev/work/.gitconfig
[url "github-work:work-github-org/"]
insteadOf = git@github.com:work-github-org/
~ / dev /工作/ gitconfig。
[user]
email = work@work.email
只要你把所有的工作reppos放在~/dev/work和其他地方的个人物品下,git在向服务器进行拉/克隆/推操作时将使用正确的SSH密钥,并且它还将为你的所有提交附加正确的电子邮件地址。
引用:
1
2