简体   繁体   中英

How can I push to multiple GitHub accounts from my PC using git? (different repositories)

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

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

How do I solve this issue?

Solved!

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)

These are the steps I did:

1. I made an ssh key.

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

here I gave id_rsa as the file name when prompted

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

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

To copy the key

$clip < ~/.ssh/id_rsa.pub

To add new SSH key in GitHub

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

3. I made another key with the same steps but with the file name as 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. Now I cloned both repositories to my computer with the SSH link

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

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

To change SSH key

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

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

Hope this helps others.

First you'll need to add the remote repositories to your local one.

git remote add origin https://github.com/user/repo.git
git remote add origin2 https://github.com/user/repo2.git

Change origin and origin2 to what ever you want.

Then just use the the remote name you want to push the branch:

git push  <REMOTENAME> <BRANCHNAME>
git push origin2 master 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM