場景
特定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"