4

I have a personal account ericpersonal and a company account ericcompanyabc (they forced me to even though they could have add my personal GitHub account to their repos) on github.com.
I'm using a computer the company gives and spend more time on work projects so I'm primarily logged in as ericcompanyabc

I then generated 2 private and public keys as a result:

❯ ls -lrt ~/.ssh
total 72
-rw-------  1 eric.nguyen  eric.nguyen   110 Aug 31 11:03 authorized_keys.jcorig
-rw-r--r--  1 eric.nguyen  eric.nguyen  2004 Sep 13 14:16 known_hosts
lrwxr-xr-x  1 eric.nguyen  eric.nguyen    76 Sep 25 21:53 ssh_auth_sock -> /var/folders/c3/q4dw0_q90lj6_nysk_hjt8p00000gn/T//ssh-31QAwZUf1Pmz/agent.501
-rw-------  1 eric.nguyen  eric.nguyen   743 Oct 26 16:02 id_rsa2.pub
-rw-------  1 eric.nguyen  eric.nguyen  3243 Oct 26 16:02 id_rsa2
-rw-r--r--  1 eric.nguyen  eric.nguyen   746 Oct 27 16:29 id_rsa1.pub
-rw-------  1 eric.nguyen  eric.nguyen  3243 Oct 27 16:29 id_rsa1
-rw-r--r--  1 eric.nguyen  eric.nguyen   279 Oct 27 16:29 config
-rw-------  1 eric.nguyen  eric.nguyen   225 Nov  3 17:57 authorized_keys

I have to switch between the 2 accounts using the following commands but it's very tedious:

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

I've also tried using .ssh/config file but no success

❯ cat ~/.ssh/config 
Host githubcompany
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa1
    UseKeychain yes

Host githuberic
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa2
    AddKeysToAgent yes
    UseKeychain yes

What can I do to make switching between Github accounts seamless?

I'm using macOS Sierra 10.12.6

  • 1
    It’s almost like you need two user accounts and to su between them with distinct prompts and environments. I wonder if shell functions could swap the variables needed - foo work and foo home – bmike Nov 30 '17 at 3:43
  • good idea, alias gitpersonal=eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_rsa2 would suffice – ericn Nov 30 '17 at 3:49
2

The simple solution for me is to avoid ever switching my identity, so I don't have to automate the changing of the environment variables and files that provide the identity to the shell:

  1. Set up GitHub SSH for your work GitHub account e.g. ericcompanyabc.
  2. Next, on my personal GitHub repos, I simply add ericcompanyabc as a collaborator. Voila, I can now push to my personal projects (as ericcompanyabc on the company's computer) without having to switch GitHub account back and forth.

(granting ericpersonal access to work repos is impossible here of course)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .