如何将一个本地git仓库推送到两个不同的github账户



在您将此标记为重复问题之前,请注意:

另一个问题似乎相关,但我认为它不完全相同,唯一的答案张贴是完全不充分的。我尝试了Shell Code的解决方案,但无法使其工作:将两个github帐户推送到同一个repo

另一个问题有一个类似的标题(由@derek-brown误导的结果),但这个问题实际上与我的问题完全不同:将本地repo推送到多个github帐户


是这样的场景:

  • Windows 10机器使用VS Code, Git Bash和CMD。
  • C:codemyproject.gitGithub帐户#1与用户名Github -user1(电子邮件:github-user1@gmail.com)
  • Github帐户#2用户名Github -user2(电子邮件:github-user2@gmail.com)
  • Github repo #1在Github -user1下的https://github.com/github-user1/myproject
  • Github repo #2在Github -user2下的https://github.com/github-user2/myproject

本地repo有以下远程:

$ git remote -v
myremote1 git@github.com:github-user1/myproject.git (fetch)
myremote1 git@github.com:github-user1/myproject.git (push) 
myremote2 git@github.com:github-user2/myproject.git (fetch)
myremote2 git@github.com:github-user2/myproject.git (push)

我希望能够以最简单的方式将这个repo推/拉到两个远程。

到目前为止,我已经完成了以下工作:

  1. 为两个身份创建ssh密钥:

    • id_ed25519_github_user1 for github-user1@gmail.com
    • id_ed25519_github_user2 for github-user2@gmail.com
  2. 添加ssh代理的身份:

$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519_github_user1
$ ssh-add ~/.ssh/id_ed25519_github_user1
  1. 将公钥添加到相应github帐户的SSH keys部分,如下所示:https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

  2. 在my ~中增加了一个配置文件。包含以下内容的SSH文件夹:

#github-user1 account
Host github-user1
Hostname github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_user1
#github-user2 account
Host github-user2
Hostname github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_user2

当我尝试推送到任何一个远程时,我得到一个这样的错误:

$ git push myremote1 main
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.      
Please make sure you have the correct access rights
and the repository exists.

好了,我通过改变遥控器让它工作了。

[清晰版:我正在修改在.ssh/config中声明的本地用户,使它们与github用户不相同,因为它们不必在一般情况下相同]

考虑以下更新的.ssh/config文件:

#github-user1 account
Host local-user1
Hostname github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_user1
#github-user2 account
Host local-user2
Hostname github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_user2

则远程更改为:

$ git remote -v
myremote1 git@github.com:github-user1/myproject.git (fetch)
myremote1 git@github.com:github-user1/myproject.git (push) 
myremote2 git@github.com:github-user2/myproject.git (fetch)
myremote2 git@github.com:github-user2/myproject.git (push)

:

$ git remote -v
myremote1 git@local-user1:github-user1/myproject.git (fetch)
myremote1 git@local-user1:github-user1/myproject.git (push) 
myremote2 git@local-user2:github-user2/myproject.git (fetch)
myremote2 git@local-user2:github-user2/myproject.git (push)

请注意,我已经更改了主机名后面的"@"到我在配置文件中输入的~。ssh文件夹。

这就是对我有效的解决方案。

然而,正如尊敬的@VonC所指出的那样,git@部分在远程中是不必要的,因为git已经在~/.ssh/config文件中指定为User git。因此,我理解它们可能如下:

$ git remote -v
myremote1 local-user1:github-user1/myproject.git (fetch)
myremote1 local-user1:github-user1/myproject.git (push) 
myremote2 local-user2:github-user2/myproject.git (fetch)
myremote2 local-user2:github-user2/myproject.git (push)

我还没有测试过这个,但我相信它会一样的工作。

实际使用的命令有:

cd /path/to/repository
git remote set-url myremote1 github-user1:github-user1/myproject.git
git remote set-url myremote2 github-user2:github-user2/myproject.git

您不需要git@部分,因为它已经在~/.ssh/config文件中指定为User git

最新更新