2

I have 2 GitHub accounts and an ssh key for each of them. Unfortunately ssh-agent always uses the first key unless I use my 3rd solution but then I can't specify the username for GitHub.

Here is my .bashrc which starts up ssh agent and adds those keys
/usr/bin/keychain --nogui $HOME/.ssh/account1 &> /dev/null;
/usr/bin/keychain --nogui $HOME/.ssh/account2 &> /dev/null;
source $HOME/.keychain/$HOSTNAME-sh &> /dev/null;

Here are the multiple things I've tried for my ~/.ssh/config

  1. I based it off this https://superuser.com/a/1525769
Host account1.git
  User git
  HostName github.com
  IdentityFile ~/.ssh/account1.pub

Host account2.git
  User git
  HostName github.com
  IdentityFile ~/.ssh/account2.pub
  1. https://superuser.com/questions/357602/use-a-specified-key-from-ssh-agent
Host github.com-account1
    HostName github.com
    User git
    IdentityFile ~/.ssh/account1

Host github.com-account2
    HostName github.com
    User git
    IdentityFile ~/.ssh/account2

  1. Only this one works but this solution isn't acceptable since my host is just github.com

https://www.freecodecamp.org/news/how-to-manage-multiple-ssh-keys/

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/account1
  IdentitiesOnly yes

Here is the error

ERROR: Repository not found.
fatal: Could not read from remote repository.

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

Also I tried this which works

ssh -T [email protected]

These other ssh calls don't work

ssh -T git@github-account1
ssh -T [email protected]
ssh -T [email protected]

I suspect that I'm not using the correct host but I honestly don't know why it would be incorrect.

Here's my GitHub account https://github.com/xuhu55

so you can substitute xuhu55 where account1 appears.

6
  • 1
    do you mean the parameter after -T is account name, not target host? Commented Jul 12, 2021 at 3:50
  • @LeiYang I thought you would have account name and target host after -T? For example ssh -T [email protected] has git as the account name and github.com as target host. Apologies if I don't understand Commented Jul 12, 2021 at 4:00
  • @Raptor Unfortunately I tried that but none of those are working. For some reason I can't seem to add xuhu55 into my Host correctly even when I tried all their suggestions. Commented Jul 12, 2021 at 4:01
  • @developer so you know the string after @ is the host, not the accout name, why do you expect github-account1.com or xuhu55.github to work, is there such host? Commented Jul 12, 2021 at 4:10
  • 1
    @Raptor I looked more thoroughly and one of the solutions worked. Commented Jul 12, 2021 at 4:21

2 Answers 2

2

This specific ended up solving it. https://stackoverflow.com/a/56067132/16428455

So I ended up needing to do this

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/account2

Host xuhu55
  HostName github.com
  User git
  IdentityFile ~/.ssh/xuhu55

This is the thing that solved it for me.

git remote set-url origin git@xuhu55:xuhu55/Project3.git

Apparently my old ssh url origin had to be changed. I had to change github.com to xuhu55 to get it to work for me. This is very easy to overlook if you've already cloned the project with ssh.

[email protected]:xuhu55/Project3.git
Sign up to request clarification or add additional context in comments.

Comments

1

Short Answer:

  1. Add email to git config locally
  2. Invite email to become a collaborator of the git repo
  3. Create ssh-key with email
  4. Add SSH-Key from .pub to GitHub account
  5. Test
  6. Connect

Boring stuff:

Use ssh-add to select from your saved ssh-keys. I believe they are stored in .ssh if you're using Linux and in User/ if you're on Windows. Example:

ssh-add ~/.ssh/myWorkGitHub
// enter password

Check if you are using the same email you used when generating the ssh by:

git config --list

if not, set the right configuration for your git.

Note: You should have added the contents of myWorkGitHub.pub to your GitHub account in order for the above code to work.

Ensure ssh is enabled:

eval "$(ssh-agent -s)"
// will return a pid if successful

Test connection:

ssh -T [email protected]
//successful message from GitHub

If your repo is private, you should invite this email to become a collaborator of the repo.

All good? Now you can connect to the repo remotely.

git push -u origin main
[email protected]:{YOUR_GITHUB_USERNAME}/{YOUR_REPO_NAME}.git

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.