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 "[email protected]" -f "github-work"
ssh-keygen -t rsa -C "[email protected]" -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.

Read more at https://www.ssh.com/ssh/keygen/


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/github-work.pub
pbcopy < ~/.ssh/github-rd.pub

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
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-work
#personal account
Host github.com-rd
    HostName github.com
    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 [email protected]{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 github.com-pingu

git clone [email protected]:penguin/daily-fur-cleaning.git

b) Updating remote of already clone repositories

Use following command:

git remote set-url origin [email protected]{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 user.name "FirstName LastName"
git config --global user.email "[email protected]"

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

git config --local user.name "rd"
git config --local user.email "[email protected]"

HIH πŸ‘

Helpful?

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

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

lets connect viatwitter