Using Multiple GitHub Accounts on the Same Login

If you want to use multiple GitHub accounts, for example, one for your work and one for your hobby projects, you can do so on the same login account. Here’s a step-by-step guide on how to set this up.

Step 1: Generate Separate SSH Keys

To keep your work and hobby accounts separate, you need to have separate SSH keys for each of them. You can generate these keys using the ssh-keygen tool. In this example, we’ll create Ed25519 keys and associate an email address with each key to associate it with your GitHub account.

	$ ssh-keygen -t ed25519 -C "johndoe@company.com"

	Generating public/private ed25519 key pair.
	Enter file in which to save the key (/home/john/.ssh/id_ed25519): johndoe-company
	Enter passphrase (empty for no passphrase): ************
	Enter same passphrase again:
	Your identification has been saved in johndoe-company
	Your public key has been saved in johndoe-company.pub
	The key fingerprint is:
	SHA256:
	The key's randomart image is:
	+--[ED25519 256]--+
	|         (=^..^=)|
	+----[SHA256]-----+

	$ ssh-keygen -t ed25519 -C "johndoe@gmail.com"

	Generating public/private ed25519 key pair.
	Enter file in which to save the key (/home/john/.ssh/id_ed25519): johndoe-hobbies
	Enter passphrase (empty for no passphrase): ************
	Enter same passphrase again:
	Your identification has been saved in johndoe-hobbies
	Your public key has been saved in johndoe-hobbies.pub
	The key fingerprint is:
	SHA256:
	The key's randomart image is:
	+--[ED25519 256]--+
	|         (^(oo)^)|
	+----[SHA256]-----+

Step 2: Add SSH Keys to GitHub

Go to the GitHub settings. Click on the “SSH and GPG keys” tab. Click on “New SSH key”. Give it a descriptive title. Copy and paste the public key from the .pub file into the key field. Click on “Add SSH key”. For more detailed information, see Adding a new SSH key to your GitHub account.

Step 3: Set Up SSH Config

To set up your SSH config, create or modify your ~/.ssh/config file and add separate profiles for each of your GitHub accounts.

	# Work GitHub account
	Host github.com-company
		HostName github.com
		AddKeysToAgent yes
		IdentityFile ~/.ssh/johndoe-company
		IdentitiesOnly yes

	# Hobbies GitHub account
	Host github.com-hobbies
		HostName github.com
		AddKeysToAgent yes
		IdentityFile ~/.ssh/johndoe-hobbies
		IdentitiesOnly yes

Make sure that this file is readable only by its owner and that the private keys have the same permissions (read/write only by the owner).

Step 4: Start SSH Agent

The SSH agent is a program that keeps track of private keys used for public key authentication and automatically provides the keys as needed to ssh. To start the ssh-agent, run the following command in the terminal:

	eval "$(ssh-agent -s)"

Once the ssh-agent is running, you can add your private key to it using the ssh-add command:

	ssh-add ~/.ssh/johndoe-company

This will prompt you for the passphrase associated with your private key. After entering the passphrase, the private key will be added to the ssh-agent, and you won’t be prompted for the passphrase again for the current session.

To automatically start the ssh-agent on login, add the following code to your .bashrc file:

	#Start ssh-agent
	if ! pgrep -u "$USER" ssh-agent > /dev/null; then
	    ssh-agent -t 1h > "$XDG_RUNTIME_DIR/ssh-agent.env"
	fi
	if [[ ! -f "$SSH_AUTH_SOCK" ]]; then
	    source "$XDG_RUNTIME_DIR/ssh-agent.env" >/dev/null
	fi

This code makes sure that the ssh-agent starts up upon log in, and that only one agent process is running at a time. It also sets the lifetime of the unlocked keys to 1 hour. Without this code, you would need to enter the passphrase associated with the Github account key each time you run Git commands.

Step 5: Create Account Specific .gitconfig Files

To create account specific .gitconfig, you should make separate parent directories for each github account, for example:

	mkdir -p ~/repos/work # parent directory for work related projects
	mkdir -p ~/repos/hobbies # parent directory for personal projects

Then, in each directory, add a .gitconfig file like this:

	~/src/work/.gitconfig

	[user]
		name = johndoe-company
		email = johndoe@company.com

	[url "git@github.com-work"]
	insteadOf = git@github.com
	~/src/hobbies/.gitconfig

	[user]
		name = johndoe-hobbies
		email = johndoe@gmail.com

	[url "git@github.com-hobbies"]
		insteadOf = git@github.com

By doing this, the correct email address is automatically used and if you copy the link from the web version of Github, you don’t have to make any modifications to it.

Step 6: Including Account-Specific .gitconfig Files in the Global .gitconfig File

To make git aware of the .gitconfig files in the work and hobbies directories, add these lines to your global .gitconfig file located in your home directory:

	~/.gitconfig


	[includeif "gitdir:~/repos/work/"]
		path = ~/repos/work/.gitconfig

	[includeif "gitdir:~/repos/hobbies/"]
	path = ~/repos/hobbies/.gitconfig

Do not forget to place the trailing slash in the [includeif “gitdir:…/”] statements.

With these .gitconfig files in place, when you run Git commands in each directory, the correct email and URL will be used automatically. Keep in mind that when you use git config --global [some argument] inside these folders, it will add the value to the global .gitconfig file in your home directory, not the local .gitconfig file. Use git config --local to modify the local config on a per-repository basis, and manually edit .gitconfig files in the work and hobbies directories.

Cloning the Repositories with the Correct Configuration

With this setup, you can now easily clone the repositories under either your work or hobbies account by simply navigating to the corresponding directory and using the git clone command with the repository’s ssh link. For example:

	cd ~/repos/hobbies
	git clone git@github.com:jonhdoe-hobbies/amazingproject.git

Thanks to the configurations you’ve set up, Git will automatically clone the repository using the suffixed address specified in the SSH config file, and SSH will seamlessly rewrite the URL back to the original Github hostname. All future commits, pulls, and pushes to this repository will use the correct configurations, key, and account.

Some valuable insights

Utilizing the same email address for multiple Github accounts

It’s possible to use a single email address for several Github accounts. To do so, you can make use of the RFC 5233 subaddressing technique, as outlined in https://tools.ietf.org/html/rfc5233. The basic idea behind this technique is to add a suffix to your email address, which Github will recognize as a separate email address, but will still deliver emails to the original email address. For example, if your email is johndoe@gmail.com, by using the subaddressing technique, it can become johndoe+hobbies@gmail.com. This could be particularly useful if you want to differentiate between the emails you receive from your different Github accounts.

Removing SSH keys

At times, you might want to remove SSH keys from the ssh-agent. This can be done using the ssh-add command with the -d or -D flag. The -d flag, along with the [path-to-the-key] argument, is intended to delete a single key. However, this flag may not work properly on your system. On the other hand, the -D flag deletes all keys from the ssh-agent. It’s worth noting that neither of these flags physically deletes the keys from your system. To completely delete a key, you need to use the rm command on the corresponding key files.

All in all, by following the steps outlined in this guide, you’ll be able to easily manage multiple Github accounts, each with its own email address and SSH key, from a single computer. This could be incredibly useful if you work on personal and professional projects, or if you simply have multiple Github accounts for different purposes.

References

https://gist.github.com/yinzara/bbedc35798df0495a4fdd27857bca2c1 (very insightful!) https://wiki.archlinux.org/title/SSH_keys