Home > OS >  Using github and gitbash with multiple users & ssh identities
Using github and gitbash with multiple users & ssh identities

Time:06-08

I would like to consult the community on how should I set up my terminal for it to automatically use the correct identity file when pushing to a repro.

Currently, what I am trying is making use of ssh config files:

~/ssh/config

# First account
Host github.com:my_first_github_account
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_ed25519_user1
   
# Second account
Host github.com:my_second_github_account  
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_ed25519_user2

I map whichever git user-specific endpoint to its identity file, and it so works when authorizing myself on it:

> ssh [email protected]:my_first_github_account
Hi my_first_github_account! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

> ssh [email protected]:my_second_github_account
Hi my_second_github_account! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

and my current issue is that when pushing with git to a remote origin (which is of course authorized to do so) it uses the wrong identity file, not the one that should of been considered:

> git config --get remote.origin.url
[email protected]:my_first_github_account/my-repo.git
> git push
ERROR: Repository not found.
fatal: Could not read from remote repository.

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

CodePudding user response:

The :account_name part should not be part of the Host name in your .ssh/config.

Apparently, command line ssh is ok with having a hostname which contains : (I didn't know that), but git extracts the github.com part and runs an ssh command which tries ssh github.com -- and your .ssh/config does not contain any config for github.com alone.


The fix is simple :

change your Host values, e.g to Host github.work and Host github.perso, and use :

git clone github.work:username/repo
git clone github.perso:username/repo

for the sake of completeness : you could work with the hostnames you defined, just use the longer url syntax to target your github repo :

git clone ssh://github.com:username/username/repo.git

but it's way more convenient to choose a host name which works with the short url form.

  • Related