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配置,而不仅仅是在回购级别。
在我的例子中,所有的项目都在~/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。
我已经在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
正如其他人提到的,核心。sshCommand config可以覆盖SSH密钥和其他参数。
下面是一个示例,其中有一个名为~/的替代键。Ssh /workrsa,并希望将其用于~/work下克隆的所有存储库。
在~/work下创建一个新的.gitconfig文件:
[core]
sshCommand = "ssh -i ~/.ssh/workrsa"
在全局git配置中~/。gitconfig,添加:
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig