Skip to content

Setting up multiple Git accounts

Two birds perching on tree branches

In this article, I set up 2 Git accounts on my device; 1 personal account and 1 work account. Both accounts use SSH keys for authentication.

This method works on Linux and MacOS. It might work on Windows but I haven't tested it.

Generate SSH keys

I'm using ssh-keygen to generate a SSH key.

ssh-keygen -t ed25519

The SSH key's file name is only for reference purposes. So, I choose key_personal. When prompted for a password for the file, my suggestion is to add one.

I repeat this step for my work account and choose key_work as the SSH key's file name.

The following files are automatically created in ~/.ssh.

  • key_personal

  • key_personal.pub

  • key_work

  • key_work.pub

The files ending with .pub contain public keys, i.e. they can be added to your GitHub account (or any other Git host, e.g. GitLab). The other files, i.e. key_personal and key_work contain private keys.

Add SSH keys to GitHub

In this step, I open each .pub file and copy the contents, i.e. the public key, and add it to the corresponding GitHub account. Follow these instructions to add the SSH keys to GitHub.

Now both GitHub accounts, i.e. personal and work, are aware of the keys that have been generated on my device.

If you're using any other Git host, e.g. GitLab, you'll need to find out how to add SSH keys in their docs.

Configure SSH

Next up, I need to open ~/.ssh/config and add the entries for each account.

Host personal
	Hostname github.com
	PreferredAuthentications publickey
	UpdateHostKeys no
	IdentitiesOnly yes
	IdentityFile ~/.ssh/key_personal

Host work
	Hostname github.com
	PreferredAuthentications publickey
	UpdateHostKeys no
	IdentitiesOnly yes
	IdentityFile ~/.ssh/key_work
  • Host - the alias I'll use later to point to the SSH key I want to use

  • Hostname - the Git host's domain name

  • PreferredAuthentications - the authentication method, e.g. password, publickey

  • UpdateHostKeys - whether to accept notifications of additional host keys from the server

  • IdentitiesOnly - whether to try all other keys in ~/.ssh

  • IdentityFile - the private key's file path

If you're curious about other options, check out this link.

I need to test the connection by running the following commands.

ssh -T git@personal
ssh -T git@work

If the above don't work, try using the hostname instead of the host.

ssh -T git@github.com

At this point, my device has the appropriate rights to communicate with both GitHub accounts. However, if I clone a repository, I won't have the correct username and email configured in Git. So, every time I have a new repository on my device, I will need to manually set up the information.

Configure Git

Here's how I set up Git to automatically configure the correct username and email for each of my Git accounts.

I need to create 1 file for each account, containing the corresponding user information.

The following content goes into ~/gitconfig_personal.

[user]
	name = n-d-r-d-g
	email = ndrdg@personal.com

The following content goes into ~/gitconfig_work.

[user]
	name = MyLegalName
	email = ndrdg@work.com

The name and email specified in these files are linked to each commit, i.e. they are visible to anyone who has access to the repositories you're committing to. You can use a private email address instead.

These files then need to be linked in ~/.gitconfig by adding the following lines.

[includeif "hasconfig:remote.*.url:git@personal:*/**"]
	path = ~/.gitconfig_personal
[includeif "hasconfig:remote.*.url:git@work:*/**"]
	path = ~/.gitconfig_work

When setting the remote URL of any private repository, the host needs to be used instead of the hostname.

E.g. use

git clone git@work:company/repo.git

instead of

git clone git@github.com:company/repo.git