git使用指定ssh key
场景
特定repo需要指定ssh private key进行访问,希望对特定repo使用独立的ssh-key便于管理。
实现
官方文档指出,可通过设定core.sshCommand影响ssh行为,从而指定private key
core.sshCommand
If this variable is set, git fetch and git push will use the specified command instead of ssh when they need to connect to a remote system. The command is in the same form as the GIT_SSH_COMMAND environment variable and is overridden when the environment variable is set.
最终命令如下
git clone ssh://git@provider.com:userName/projectName.git --config core.sshCommand="ssh -i ~/location/to/private_ssh_key"
进阶
当然,对于需要固定key的仓库可以直接写入.git/config
一劳永逸
效果:
git config core.sshCommand "ssh -i /dev/zero"