Multiple GitHub accounts on macOS

Situation is to use my work and personal GitHub accounts on the same machine.

Steps I'v followed:

1. Generate ssh keys

Generate the ssh keys as follows:

cd ~/.ssh
ssh-keygen -t rsa -b 4096 -C "" -f "github-work"
ssh-keygen -t rsa -C "" -f "github-rd"

-b β€œBits” This option specifies the number of bits in the key. The regulations that govern the use case for SSH may require a specific key length to be used. In general, 2048 bits is considered to be sufficient for RSA keys.
-c "Comment" Changes the comment for a keyfile. Helps in identification of the key file.
-f "File" Specifies name of the file in which to store the created key.


2. Add the ssh keys to ssh-agent

Add the keys to ssh-agent as follows:

ssh-add -K ~/.ssh/github-work
ssh-add -K ~/.ssh/github-rd

-K is to store passphrase in macOS' keychain.
Read more at GitHub Docs

3. Add the ssh keys to respective GitHub accounts

Next step is tell GitHub about the fresh keys. So add them to the respective GitHub accounts by visiting GitHub Settings.

Use following command to copy the public key' content in the clipboard.

pbcopy < ~/.ssh/
pbcopy < ~/.ssh/

You can also follow GitHub Docs for this step.

4. Configure ssh to use appropriate keys

Tell ssh, when to use which key by the means of config file in ~/.ssh directory.

Here's what I've for my case:

#work account
    User git
    IdentityFile ~/.ssh/github-work
#personal account
    User git
    IdentityFile ~/.ssh/github-rd

Trick here is the Host entry.
Which is acting as the nickname and resolves to the actual domain using HostName entry.

So you use a specific nickname, while interacting with GitHub repositories. Which signals the ssh, which key file to pick for authentication.

Host vs HostName at SO

5. Working with GitHub repositories

a) Cloning new repo!

Clone the repo as follows specifying the nickname against which you want ssh-agent to pick the key file.

git clone{nickname}:{repo-owner-name_can-be-org-or-user-name}/{repo-name}.git

Example: If I wish to clone the daily-fur-cleaning repo from the user penguin, whose key file is configured against Host

git clone

b) Updating remote of already clone repositories

Use following command:

git remote set-url origin{nickname}:{repo-owner-name_can-be-org-or-user-name}/{repo-name}.git

Bonus! What about name and email for commits?

Have one set configure to be used globally as below:

git config --global "FirstName LastName"
git config --global ""

and then have local configs in every repo for secondary accounts.

git config --local "rd"
git config --local ""

HIH πŸ‘


If you think this is helpful 🎈
Don't keep it to yourself πŸ™Š

Share it with your lovely followers at twitter πŸ—½


powered by Β
lets connect viatwitter