2

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 Shell Code's 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 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)

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:

    • id_ed25519_github_user1 for github-user1@gmail.com
    • id_ed25519_github_user2 for github-user2@gmail.com
  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
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.
freenrg
  • 99
  • 6

2 Answers2

1

OK, I got it to work by changing the remotes.

[Edition for clarity: I am modifying the local users declared in the .ssh/config, so that they are not the same as the github users, as they do not have to be identical in general]

Consider the following updated .ssh/config file:

#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

Then the remotes are changed from:

$ 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)

To:

$ 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)

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

That is the solution that has worked for me.

However, as pointed out by the respected @VonC, the git@ part is not necessary in the remotes, because git is already specified in the ~/.ssh/config file, as User git. Therefore, I understand they could be as follows:

$ 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)

I have not tested this, but I trust it will work the same.

freenrg
  • 99
  • 6
0

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-user2/myproject.git

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250