How to Configure Multiple Git Accounts on macOS?

On macOS, you can set up multiple git accounts on the same local machine in the following steps:

  1. Generate SSH public/private key pairs for each of your git accounts using ssh-keygen command;
  2. Copy the generated SSH public key and add it to your git account settings;
  3. Edit (or create) ~/.ssh/config file to add git host entries for each of your git accounts using a text editor (such as nano or vim);
  4. Optionally, test the keys by running the "ssh -T git@host-name" command (where the host name is the one you specify for the Host keyword in the ~/.ssh/config file).

For example, let's suppose you want to use two of your GitHub accounts on the same macOS system, you can do so in the following way:

  1. Generate SSH keys;
  2. Copy public SSH key and add to git;
  3. Set up SSH profiles;
  4. Test the keys.

Generating SSH Keys

You can generate SSH for two separate GitHub accounts in the following way:

# 1: generate ssh keys

ssh-keygen -t rsa -C "[email protected]" -f "github-personal"
ssh-keygen -t rsa -C "[email protected]" -f "github-work"

In these commands, the following options are used:

  • -t: used to specify the type of key to create;
  • -C: used to add a comment (which is appended at the end of the public key);
  • -f: used to specify the file name for the generated keys.

When you run both these commands, it will generate two SSH public/private key pairs in the ~/.ssh/ folder having the name "github-personal" and "github-work" (as specified by the -f option).

Copying Public SSH Key and Adding to Git

After generating the SSH keys, you need to copy the public keys one by one and add each one to their respective GitHub account, for example, like so:

# 2: copy public ssh key and add to github

pbcopy < ~/.ssh/github-personal.pub
# ... paste the copied key to "personal" account ...

pbcopy < ~/.ssh/github-work.pub
# ... paste the copied key to "work" account ...

Setting Up SSH Profiles

After copying the public keys over to the relevant git accounts, you need to set up two SSH profiles (one for each of the two git accounts) by modifying (or creating) ~/.ssh/config file, for example, like so:

# 3.1: edit ~/.ssh/config file

open -t ~/.ssh/config

If the file does not already exist, then you can create it first using the touch config command.

This command will open the SSH config file in your default text editor. Once it's open, you can configure the git host entries for each of your account, for example, like so:

# 3.2: add git host entries for each of your git accounts

Host personal.github.com
   HostName github.com
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/github-personal
   IdentitiesOnly yes

Host work.github.com
   HostName github.com
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/github-work
   IdentitiesOnly yes

The configuration in the example above will set up two distinct git profiles, where the specified options do the following:

  • Host: This specifies the nickname of the host you are connecting to.
  • HostName: This specifies the actual hostname you are connecting to.
  • AddKeysToAgent: This specifies whether to automatically add the SSH key to the "ssh-agent" when you run the ssh command. Setting this to "yes" adds the SSH key for the related host to the ssh-agent when you first connect to the host.
  • UseKeychain: This specifies whether to use the macOS keychain to store the SSH key passphrase. Setting this to "yes" stores the passphrase in the keychain, so you don't have to enter it every time you use the key.
  • IdentityFile: This specifies the path to the private SSH key file that should be used for authentication.
  • IdentitiesOnly: This specifies whether SSH should only use the identities specified by IdentityFile, instead of trying all available identities. Setting this to "yes" will make SSH only use the specified key for authentication, rather than trying all the keys in your ~/.ssh directory.

Please note that if you have any pre-existing repositories, you may have to set the git remote URL to match the Host nickname(s) you specified. Otherwise, you might get a "Permission to remote/repo.git denied to <username>" error.

Testing the Keys

You can test if the keys you added work, for example, by using the Host name you specified with the following command:

ssh -T [email protected]
# Hi <name>! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T [email protected]
# Hi <name>! You've successfully authenticated, but GitHub does not provide shell access.

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.