- 产生 key
ssh-keygen
场景一
- 不同git server,不同 ssh key
编辑文件 ~/.ssh/config
Host <Host Name>
HostName AAAAAAA.org
IdentityFile ~/.ssh/<key for the given host>
Host <Host1 Name>
HostName BBBBB.org
IdentityFile ~/.ssh/<host 2 key>
场景二
- 相同git server,不同用户,不同ssh key
git config --local core.sshCommand 'ssh -i private_key_file'
- 或者直接编辑 .git/config ,在 core 下面加入
sshCommand = ssh -i ~/.ssh/harryhare
user@workstation:~/workspace/project-name/.git$ cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
sshCommand = ssh -i ~/location-of/.ssh/private_key -F /dev/null <--Check that this command exist
[remote "origin"]
url = git@github.com:<user-name>/<project-name>.git <-- Make sure its the SSH URL and not the WEB URL
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
参考:https://stackoverflow.com/questions/4565700/how-to-specify-the-private-ssh-key-to-use-when-executing-shell-command-on-git