如何为 Git 指定 SSH 密钥文件
如果你只希望对单个 git server 配置独立的 ssh key,可以在 ~/.ssh/config
里面加上如下配置即可:
1 | Host github.com |
以下内容为转载:
如果你在运行 ssh 命令时想指定一个密钥文件,一种很简便的方法就是使用 -i 选项。
ssh -i ~/.ssh/thatuserkey.pem thatuser@myserver.com
这样非常干净利索。既简单、优雅,又十分直观。对 git 命令,我也想这么做:
git -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git
然而,git 命令并没有这样的 -i 选项。真糟糕!
我找了很久都没找到类似的解决办法。我只能想到如下两种备选方案:
- 使用 GIT_SSH 环境变量
- 使用包裹脚本(wrapper script)