There may come a point in your life where you will have to manage multiple git profiles, one for work and one for personal reasons. You will only encounter this problem if you say you have your personal projects and work projects with the same Git hosting, commonly GitHub. But the same applies to GitLab or Bitbucket or for any other hosting companies.

If this is the case, then it can be a bit of a nightmare when it comes to swapping credentials depending on the repository.

There is of course a quick workaround. Use a single GitHub account, and use them for both work and personal. This works and it’s a personal choice. For those that want to keep things clean and use them separately, then read on.

In this post, I will outline out how to create two Git profiles for GitHub (the same applies for GitLab or Bitbucket or whatever really) so you can separate your personal and work profiles.

Create SSH Keys

The first step will be to create SSH keys. We’ll need to create two SSH keys. One for work and one for personal. For now, let’s create one for work. The steps to create an SSH key for personal purposes are identical. The only difference is when we come to name the stored SSH key, which is in step 5.

1. Launch a terminal

For Windows users, launch Git Bash, for Mac, launch terminal.

2. Navigate to the .ssh directory

For Windows:

cd C:\Users\[YOUR-WINDOWS-USERNAME]\.ssh

For Mac:

cd ~/.ssh

3. Generate an SSH key In the terminal, enter the ssh-keygen command to generate a new ssh key. Now, you may have already registered with GitHub, or GitLab with an email address, such as [email protected]. This will be your username.

ssh-keygen -t rsa -b 4096 -C "[email protected]"

4. Enter a file name When it prompts to “Enter a file”, something like this:

Enter file in which to save the key (/c/Users/[you-username]/.ssh/id_rsa):

Enter a filename of your choice. This is so that you can name your ssh keys for personal and work purposes. For example, you can name it: id_rsa_github_work for work and id_rsa_github_personal for personal. Something like this:

Enter file in which to save the key (/c/Users/[you-username]/.ssh/id_rsa): id_rsa_github_work

5. Enter a passphrase Now, it will prompt you to enter a passphrase. Something like this:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

This is optional. You can skip this step and just hit enter key to continue. But feel free to enter a password here.

6. Repeat You can either stop here or repeat steps 1 -5 to create another SSH key for say your personal account, and you may want to call the file id_rsa_github_personal.

Verify the SSH key is generated

It should have created two new files in the default .ssh directory. For Windows it will be C:\Users\[YOUR-WINDOWS-USERNAME]\.ssh, for Mac it will be ~/.ssh

  • id_rsa_github_work
  • id_rsa_github_work.pub

Add the SSH key to GitHub or GitLab

Now that we have a SSH key, we need to copy the contents of the *.pub file into the clipboard. In the example above, this will be the id_rsa_github_work.pub file.

1. Copy the SSH key Open the file and copy the entire contents of the file. A little Ctrl+A and Ctrl+C.

2. Add the SSH Key to GitHub or GitLab Depending on if it’s GitHub or GitLab, you can visit the GitHub page of adding a new ssh key, or for GitLab, you can visit the GitLab page for adding new ssh key.

Just follow the steps of how to add the SSH key, and you can ignore the steps of generating the SSH key as we have done this in the previous steps.

As we have two SSH keys, we will need to upload both. One for work and the other for personal.

Modify the Git config to handle multiple profiles

1. Edit the Git config file

There should be a file called config located in the .ssh directory.

For Windows, its located in C:\Users\[YOUR-WINDOWS-USERNAME]\.ssh

For Mac, its located in ~/.ssh/

If there isn’t one, create one in an editor of your choice.

2. Add profiles

In the config file add the following configuration.

# GitHub (work)
Host work #<----------- take note of this Host value.
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github_work

Take note of the second line where it says Host xxx. In the above, it’s Host work. You can name this anything such as Host xyz. Just make a note of what this is as it will be needed in Testing it Out section.

If you are also adding a personal profile, then it will something like this:

# GitHub (personal)
Host personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github_personal

# GitHub (work)
Host work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github_work

Ensure SSH identities are loaded

To ensure the SSH keys generated are loaded into the terminal, we verify this with a very simple command.

This command will list all SSH keys loaded. For Windows, you can have a couple of options. One is enabling ssh-agent in using this StackOverflow link. Alternatively, you can use cmdr which makes life way more simple.

For Mac, you’ve got it easy, just type the following in the terminal:

ssh-add l

If the identities are not listed we can add them using the following command.

For Windows

ssh-add "C:\Users\[your-username]\.ssh\id_rsa_github_work" 

For Mac

ssh-add "~.ssh/id_rsa_github_work" 

Testing it out

Now, let’s say you have a new repository that you wish to clone for work, the SSH path may look something like this:

[email protected]:companyA/my-repo.git

To clone it, we will need to use the profiles we created in Modify the Git config to handle multiple profiles section. We have to replace the [email protected] with git@work. The work part is what we called it as part of the Git config file Host work. If I had said Host CompanyA, then I would need to replace [email protected] with git@CompanyA.

So it will something like this:

git clone git@work:companyA/my-repo.git

If you have an existing repo, you can switch the remote URLs using githubs.

That’s it. That’s how you can have multiple Git profiles and multiple Git repositories with specific profiles depending on whether it’s for work or personal purposes.

I hope that has helped as it has caused me much grief and pain and rabbit holes when trying to juggle with multiple profiles.