Setup Multiple Github Accounts
Sometimes you need to use more than one account on your machine, whether it’s for work purposes or other reasons.
How to add multiple GitHub accounts?
You can use ssh keys and define host aliases in ssh config file (each alias for an account). For archieve this you can follow the next steps.
-
Generate ssh key pairs for accounts and add them to GitHub accounts.
-
Set up in your ~/.ssh/config with different host for different accounts. If you don’t have the file you’ll need to create it.
In this example we use github.com for the work account and me.github.com for our personal account.
# Defaul Account // Work_Account Host github.com User work-user AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa_work # key path # Personal_Account Host me.github.com HostName github.com User personal-user AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa_personal -
Check your repository keys.
$ ssh-add -l -
Add Keys. Add your personal and work keys.
$ ssh-add ~/.ssh/id_rsa_work $ ssh-add ~/.ssh/id_rsa_personal -
Test your connection. check your connection to both hosts.
$ ssh -T git@github.com $ ssh -T git@me.github.com -
Now you can use both accounts in the same machine.
$ git clone git@me.github.com:org2/project2.git /path/to/project2 $ cd /path/to/project2 -
[WARNING] To avoid making commits with the wrong user, you need to be careful. To prevent this, follow the configuration steps below.
$ git config user.name "work-user" # Updates git config user name $ git config user.email "work-user@workmail.com"Additionally, you can change the global (default) machine user.
$ git config --global user.name "work-user" $ git config --global user.email "work-user@workmail.com"
That’s all folks!