如何使用git从我的PC推送到多个GitHub帐户? (不同的存储库)

问题描述:

是否可以在多个帐户中推送到不同的远程存储库?我在GitHub上有2个帐户,每个克隆到我的PC中都有1个存储库.我在两个存储库中都进行了提交,我想将它们分别推送到其各自的远程服务器.对于第一个存储库,当我将其推送到遥控器时,它会要求输入用户名和密码.但是稍后,当我尝试将第二个存储库推送到其远程存储库时,它显示为permission denied,它甚至不要求任何授权,并给我这个错误

Is it possible to push to different remote repositories in multiple accounts? I have 2 accounts in GitHub and I have 1 repository from each cloned into my PC. I made commits in both repos, and I want to push both separately to its respective remote. For the first repo, when I pushed it to the remote, it asked for the username and password. But later when I tried to push the second repo to its remote repo, it says permission denied and it doesn't even ask for any authorization and gives me this error

remote: Permission to anasbasheer/anasbasheer.github.io.git denied to an4s911.
fatal: unable to access 'https://github.com/anasbasheer/anasbasheer.github.io.git/': The requested URL returned error: 403

此后,我尝试使用SSH密钥,但没有任何作用.

After this I tried using SSH keys but it didn't make a difference.

我该如何解决这个问题?

How do I solve this issue?

已解决!

我为两个帐户都设置了2个不同的ssh密钥,现在每次我要推送到特定帐户时,我都必须不断更改ssh密钥. (至少有总比没有好)

I made 2 different ssh keys for both the accounts and now each time I want to make a push to a particular account I have to constantly change the ssh key. (at least something is better than nothing)

这些是我执行的步骤:

1..我按下了ssh键.

1. I made an ssh key.

$ssh-keygen -t rsa -C 'name@email.com'

在出现提示时,我给id_rsa作为文件名

$eval $(ssh-agent -s)
$ssh-add ~/.ssh/id_rsa

2..我在我的主要GitHub帐户中复制了密钥并添加了新的SSH密钥

2. I copied the key and added new SSH key in my main GitHub account

要复制密钥

$clip < ~/.ssh/id_rsa.pub

要在GitHub中添加新的SSH密钥

To add new SSH key in GitHub

转到GitHub>右上角的下拉菜单>设置> SSH和GPG密钥>新的SSH密钥>在其中粘贴密钥

Go to GitHub > Drop-down menu in the top right corner > Settings > SSH and GPG keys > New SSH key > paste the key there

3..我用相同的步骤制作了另一个键,但文件名为id_rsa_2

3. I made another key with the same steps but with the file name as id_rsa_2

现在在使用id_rsa的前两个步骤中的任何地方,都应将其替换为id_rsa_2

Now everywhere in the first 2 steps where id_rsa is used it should be replaced with id_rsa_2

然后我将此密钥添加到了我的第二个帐户.

And then I added this key to my second account.

4..现在,我使用SSH链接将两个存储库克隆到了我的计算机上

4. Now I cloned both repositories to my computer with the SSH link

$git clone git@github.com:username/repo-name.git

5.就这样,现在每次我想推送到其中一个帐户时,我都必须更改ssh键

5. That's it and now each time I want to push to one of the accounts I have to change the ssh key

更改SSH密钥

$ssh-add ~/.ssh/your-key

在我的情况下,

your-key将是id_rsaid_rsa_2

in my case, your-key will be either id_rsa or id_rsa_2

希望这对其他人有帮助.

Hope this helps others.