Home > Software engineering >  How to push one single local git repo to two different github accounts
How to push one single local git repo to two different github accounts

Time:12-10

Before you jump to flag this as duplicate question, please note:

This other question seems related, but I believe it is not exactly the same and the only answer posted is completely insufficient. I tried the "solution", but could not make it work: Two github accounts to push to same repo

This other question has a similar title (the result of misleading edition by @derek-brown), but the question is actually completly different from mine: Pushing a local repo to multiple github accounts


This is the scenario:

The local repo has the following remotes:

$ git remote -v
myremote1 [email protected]:github-user1/myproject.git (fetch)
myremote1 [email protected]:github-user1/myproject.git (push) 
myremote2 [email protected]:github-user2/myproject.git (fetch)
myremote2 [email protected]:github-user2/myproject.git (push)

I want to be able to push/pull this repo to both remotes at will in the simplest possible way.

I have so far done the following:

  1. Created ssh keys for both identities:

  2. Added the identities to the ssh agent with:

$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519_github_user1
$ ssh-add ~/.ssh/id_ed25519_github_user1
  1. Added the public keys to the SSH Keys section of the corresponding github account, as explained here: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

  2. Added a config file in my ~.ssh folder with the following content:

#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

When I try to push to either remote I get an error like this:

$ git push myremote1 main
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.      

Please make sure you have the correct access rights
and the repository exists.

CodePudding user response:

OK, I got it to work by changing the remotes from:

$ git remote -v
myremote1 [email protected]:github-user1/myproject.git (fetch)
myremote1 [email protected]:github-user1/myproject.git (push) 
myremote2 [email protected]:github-user2/myproject.git (fetch)
myremote2 [email protected]:github-user2/myproject.git (push)

To:

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

Please note I have changed the hostnames after the "@" to the ones I had entered in the config file in the ~.ssh folder.

CodePudding user response:

The actual command to use are:

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

You do not need the git@ part, since it is already specified in the ~/.ssh/config file, as User git.

  • Related