我们在使用 AndroidStudio 时可能不同的 Project 对应不同的 GitHub 账号。
如果我们要使用 SSH 应该如何配置呢?
我们需要明确 SSH 秘钥就是一把钥匙,在项目中Git需要知道去拿哪把钥匙去开门。
那么我们的钥匙放在哪里呢?我一般是放在 /Users/{user name}/.ssh 目录下,两个钥匙分别命名为 id_rsa_key_1, id_rsa_key_2, 在 /Users/{user name}/.ssh 目录下新建 config 文件。文件内容如下
// 别名,可自己定义 Host key_1 // github user name User user_1 Hostname ssh.github.com PreferredAuthentications publickey // 存放 key 的路径, 放在别的文件夹下也是可以的 IdentityFile ~/.ssh/id_rsa_key_1 Port 443 Host key_2 User user_2 Hostname ssh.github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_key_2 Port 443
像上面一样,我们相当告诉电脑,我在 /Users/{user name}/.ssh 路径下存放了两把钥匙 key_1,key_2 。如果有人想用可以来取。
那么在项目中我们就需要告诉 git 你需要去取哪把钥匙。配置文件项目路径/.git/config如下
// 项目 1 [remote "origin"] // 注意: 因为都在 github 中, 所以要将 git@github.com 替换为 git@key_1 // 用以告诉 git 它需要取哪把钥匙 url = git@key_1:/user_1/project_1.git fetch = +refs/heads/*:refs/remotes/origin/* // 项目 2 [remote "origin"] url = git@key_2:/user_2/project_2.git fetch = +refs/heads/*:refs/remotes/origin/*
配置完成中后, 分别在 project_1 和 project_2 中的 terminal 中输入以下两个命令, 用以测试设置是否成功。ssh -T git@key_1 和 ssh -T git@key_2 执行后分别输出
Hi user_1! You've successfully authenticated, but GitHub does not provide shell access. Hi user_2! You've successfully authenticated, but GitHub does not provide shell access.
即表示配置成功。git 终于知道去拿哪把钥匙来开门了。



