对同一个 git 远程地址使用多个凭据

Using multiple credentials for same git remote address

我有两个 git 存储库要推送

还有一个在 .ssh/config 这里

Host git-codecommit.ap-northeast-1.amazonaws.com
User AAAAAAAAAAAAA
IdentityFile ~/.ssh/id_rsa

另一个也在.ssh/config这个

#Host git-codecommit.ap-northeast-1.amazonaws.com
#User BBBBBBBBBBBBB                                                                                                                            
#IdentityFile ~/.ssh/id_rsa

两者具有相同的地址和不同的用户名

git remote -v这样显示

origin  ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/my-admin

当我这样做时git push它会上传未注释掉的用户。

所以我想在命令行中切换用户如

git push -i ~/.ssh/id_rsa BBBBBBBBBBBBB@git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/my-admin

但是它不起作用(git push 没有 -i 选项)

这样做有什么好主意吗??

git push doesn't have -i option

在您的情况下不需要,因为在两个帐户之间共享具有默认名称 id_rsa 的相同私钥。

我更喜欢使用两个单独的密钥,并且主机条目要短得多,以强调您使用的是 ~/.ssh/config 条目而不是常规 SSH URL:

Host gcaws1
  Hostname git-codecommit.ap-northeast-1.amazonaws.com
  User AAAAAAAAAAAAA
  IdentityFile ~/.ssh/id_rsa

Host gcaws2
  Hostname git-codecommit.ap-northeast-1.amazonaws.com
  User BBBBBBBBBBBBB
  IdentityFile ~/.ssh/id_rsa2

并使用 URL 如:

origin  ssh://gcaws1/v1/repos/my-admin
# or
git push gcaws2:v1/repos/my-admin # use BBBB's key