Use multi git accounts on mac

One for company, one for yourself.

Why you need two accoutns

If you have both personal and company GitHub accounts, it is best practice to set up a separate SSH key for each account. This approach avoids authentication conflicts and keeps your personal and work credentials isolated

Steps

Set Up Multiple SSH Keys

ssh-keygen -t rsa -C "your_personal_email@example.com" -f ~/.ssh/id_rsa_personal

ssh-keygen -t rsa -C "your_company_email@example.com" -f ~/.ssh/id_rsa_company

add to ssh key agent

ssh-add -K ~/.ssh/id_rsa_personal
ssh-add -K ~/.ssh/id_rsa_company

Add Each Public Key to the Corresponding GitHub Account

Go to GitHub > Settings > SSH and GPG keys, and add the correct public key for each account.

Edit Your SSH Config File on Mac

~/.ssh/config and add entries for each account:

# Personal GitHub
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
IdentitiesOnly yes

# Company GitHub
Host github-company
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_company
IdentitiesOnly yes
  • Host is just a label, for you to easy remember
  • User must be git

How to use

  1. git config for current project
  2. git add remote origin git remote add origin git@github.com-projectName.git
    git config user.email "yourUserEmail"
    git config user.name "yourUserName"
    

References

Tags:

Posted: