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"

參考

https://git-scm.com/docs/git-config


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM