Let’s say you have two GitHub accounts: one is a work account that you use for professional projects, and the other is for your top secret hacker projects. This can cause problems like trying to push to a repository with an account that doesn’t have access or having the wrong GitHub author in your commits. For people with 2FA enabled, juggling ssh configs can also be hugely annoying. Here’s how I clone, pull, and push to GitHub repos using multiple GitHub accounts on one computer.
Step 1: Generate ssh keys like usual
Follow the instructions from GitHub to generate keys for each account and add them to your GitHub profiles.
You’ll be asked to specify a file to save your key in. You’ll want to give each file a name that describes which account it will be used for. It’s a good idea to call it something like
id_rsa_github_username. Make sure you include the
-K flag when running
ssh-add to add the keys to your ssh-agent. This option saves the key to your keychain so you can avoid having to enter your password again down the line.
$ ssh-add -K ~/.ssh/id_rsa_name
Step 2: Create your ssh profiles in your ssh config
After generating ssh keys, the public and private keys should appear in your
~/.ssh/ folder. Next, we have to tell git which ssh key to use when authenticating a request to GitHub. To do this, we create ssh profiles in our
# work config
# hacker config
Host: for the purposes of this tutorial, you can think of this as the name you’ll use to refer to your ssh profile later.
HostName and User: used to specify the host and username for the ssh connection. In this case, it should always say
IdentityFile: specifies the location of the private key that should be used for this ssh profile. These are the files we generated in the last step. Make sure that you’re pointing to the private key (the one with no file extension) and not the public one (the one with a
.pub file type).
UseKeychain: when set to
yes, adds the password to your keychain so you only have to verify the password once.
AddKeysToAgent: when set to
yes, the ssh key is automatically added to the running
ssh-agent whenever it is loaded. Use this setting to avoid having to ssh-add your keys every time you start a new terminal session.
Step 3: Using your ssh profiles
Once you have your profiles set up, using them is simple! Whenever you clone a repository, make sure you are using an ssh url. The url copied from GitHub should look like
Simply replace “
github.com” with the ssh profile you wish to use (I.E.
git@work:organization/repo.git) and proceed as usual!
git clone git@profile:organization/repo.git
Cloning the repo from this ssh url should set the origin appropriately, so you should be good to go for all future git operations within this repo. If something goes wrong, you can always change the remote using
git remote set-url origin git@profile:organization/repo.git
Step 4: Using multiple git configurations
Now that your ssh keys are configured properly you’ll want to set up git configurations for the author information on each of your accounts. By default all your git config settings are stored in a
~/.git/config file for all git projects on your machine. We could customize our git configuration for each and every project we clone, but that sounds really tedious. Instead, I create root folders for each of the GitHub accounts on my computer, and have git configurations set once for each folder.
To do this, first let’s make folders for each of our GitHub users. For the sake of this tutorial that means making one folder called
work and another called
hacker. Within each of these folders, create a
name = Jane Doe
email = firstname.lastname@example.org
name = J@n3 D03
email = jHAX@users.noreply.github.com
Unfortunately, this will not automatically propagate into the contained project folders on its own. To achieve this, we need to add a little config to our global
# use configuration files in each project folder
path = ~/path/to/work/.gitconfig
path = ~/path/to/hacker/.gitconfig
You’re done! When cloning a new repo, just follow these steps:
- Clone using ssh (not https)
github.com in the ssh url with the ssh profile for the GitHub account you’d like to use for this repo
git clone git@profile:organization/repo.git in the folder corresponding to the git profile you’d like to use when making commits in this repo
Sources and further reading:
Interested in more software development tips & insights? Visit the development section on our blog!