在使用git的过程当中因为要操作多个工程,需要为不同的project设置不同的用户,可以通过以下方式来进行设置
配置 ~/.ssh/config 全局设置
此方法适用于修改所有的project的git 配置
host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
User git
Environment variable GIT_SSH_COMMAND
从git 2.3.0 版本开始,可以使用环境变量 GIT_SSH_COMMAND 来进行配置
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example
注意 -i
参数有时会被config
文件锁覆盖,在这种情况下提供空的SSH
config 文件可以解决
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone example
在Git 2.10.0 版本开始,可以对每一个project 或者全局的设置git 信息, 不再需要设置环境变量配置 core.sshCommand
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
git pull
git push