读到微信公众号文章 本地如何配置多个 GitHub/Gitee 账号?,想起了几年前,面对这样的需求,我自己也是这么操作的。
当时由于一个不可细说的需求,需要向别人提供我的个人 Git 账号,用于向一个仓库提交代码。我不愿意把自己的密码提供给其别人,我就给他提供了一个新的 Key。
为了测试流程能够走通,我在本地这么测试的(就和公众号所说的差不多):
- 生成另一套 key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_another -C "AnotherKey"
-
把
~/.ssh/id_rsa_another.pub
配置到 GitHub 上 -
配置本地
~/.ssh/config
Host github.com
User git
IdentityFile ~/.ssh/id_rsa
Host githubAnother
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_another
- 测试连接:
ssh -v -T git@githubAnother
# Hi markjour! You've successfully authenticated, but GitHub does not provide shell access.
- 在仓库中配置
git remote set-url origin git@githubAnother:markjour/xyz
我之前没有这么操作过,不过我想着 Git 是走 SSH,而 SSH 是可以配置一个主机多个用户的, 然后我一试, 果然可以。
不过,对方竟然觉得配置 Key 很麻烦(这个出乎我的意料),为了避免让人觉得我太事儿了,最后还是提供了我的密码。