Enjoy your Github with SSH-use multiple Github accounts
By: Zhi Heng Sun
Tags: Github, Ssh Agent, Git, Ssh, Multiple Accounts

Available Connection Methods

Now you can clone git repository via 3 methods.

HTTPS, SSH, Github-CLI

HTTPS

# git clone https://github.com/facebook/react.git

HTTPS connection will always verify the server using certificate authorities.

It allows anonymous pull and uses password authentication for pushing.

You have to input your Github password. Once you’v authenticated successfully, your credentials are stored in the macOS keychain and will be used every time you clone an HTTPS URL.

But be careful when storing the password permanently — since it can be used to change anything in your GitHub account.

Also if you have enabled 2FA, you will have to use a personal access token instead of your regular password.

HTTPS works practically everywhere, even in places which block SSH and plain-Git protocols.

SSH

# git clone git@github.com:facebook/react.git

SSH connection uses public-key authentication. You have to generate a keypair then add it to your Github account.

It’s very useful and more secure than passwords, since you can add many to the same account (for example, a key for every computer you use GitHub from). The private keys on your computer can be protected with passphrases.

Github does not require 2FA codes either — so whoever obtains your private key can push to your repositories without needing the code generator device.

The keys are only allowed for pushing/pulling, not allowed for editing account details. If you lose the private key (or if it gets stolen), you can just remove it from your GitHub account.

But it blocks anonymous access, so you always need a GitHub account for pulling and cloning.

GitHub CLI

# gh repo clone facebook/react

GitHub CLI is a Command-line interface to the code hosting platform which can be used for issues and pull requests.

Photo by Richy Great on Unsplash

Create SSH keys for all accounts

Imagine that now you want to use 2 GitHub accounts — userA and userB.

First create ssh keys with the following commands.

# cd ~/.ssh

# ssh-keygen -t rsa -C "userA@gmail.com" -f "github-userA"

# ssh-keygen -t rsa -C "userB@gmail.com" -f "github-userB"

-C : “Comment” changes the comment for a keyfile.

-t : “Type” The option specifies the type of key to be created.

-f: “File” Specifies name of the file in which to store the created key.

For the further information, please visit here.

Now you have public and private keys in your ~/.ssh/ folder.

Add the SSH keys to your SSH-agent

We should add the SSH keys to the agent. Let’s add them.

# ssh-add -K ~/.ssh/github-userA

# ssh-add -K ~/.ssh/github-userB

-K option is Apple’s standard version of ssh-add, which stores the passphrase in your keychain for you when you add an ssh key to the ssh-agent. If you choose not to add a passphrase to your key, run the command without the -K option.

If you don’t have Apple’s standard version installed, you may receive an error.

For the more details, please read this article.

Import all the public keys on the corresponding Github accounts

Now we should register each key we created.

First copy the key to the clipboard.

# pbcopy < ~/.ssh/github-userA.pub

# pbcopy < ~/.ssh/github-userB.pub

Then go to the settings tab, click the “New SSH key” button and paste the public key from clipboard.

Create GitHub host entries for all accounts

For SSH connection, we should add config options for SSH.

~/.ssh/config file contains the config information of SSH connection.

If you don’t have this file, please create and open using the below command.

# open -e ~/.ssh/config

Edit your config file with the following contents.

#userA account

Host github.com-userA

HostName github.com

User git

IdentityFile ~/.ssh/github-userA #userB account

Host github.com-userB

HostName github.com

User git

IdentityFile ~/.ssh/github-userB

Set default GitHub user

Which account do you want to use by default?

Make userA as default.

# git config --global user.name "userA"

# git config --global user.email "userA@gmail.com"

Clone using secondary accounts