Juggling Git SSH Keys: A Tale of Two (or More) Accounts

Muntasir Billah Munna

21 May, 2024

Howdy, partner?

First things first, if you’re attempting a wild juggling act between two different platforms like GitHub and GitLab, hold your horses! This post isn’t your ticket to success, you’re in the wrong rodeo, amigo. You can simply use the same key in two different platforms. But hey, who am I to judge?

Alright, so you’re straddling different accounts in the same Git platform like GitHub or GitLab, and they don’t allow using the same SSH key for two accounts. Don’t fret! Let’s breeze through the steps of managing not one, but two SSH keys for your Git adventures.

Step 1: Generating Unique SSH Keys

First things first, make sure you’ve got separate accounts for each purpose. Assuming you’ve got accounts with emails like personal@mail.com and work@mail.com, let’s get cooking:

					mkdir $HOME/.ssh # Only if it's not there already
ssh-keygen -t ed25519 -C "personal@mail.com" -f $HOME/.ssh/key_personal
ssh-keygen -t ed25519 -C "work@mail.com" -f $HOME/.ssh/key_work

For those still rocking the Stone Age systems, if ed25519 doesn’t cut it,
just use rsa -b 4096.

And hey, Windows gang, swap out $HOME for %userprofile%.

Now you’ve got a pair of keys for each: key_personal and key_work, along with their trusty sidekicks—the public keys.

Next, let’s introduce these keys to the SSH agent:

					eval "$(ssh-agent -s)" # Raise the SSH flag!
ssh-add ~/.ssh/key_personal # Toss in your personal key
ssh-add ~/.ssh/key_work # And now the work key

Note: Still wrangling the rope on generating SSH key and adding it to your SSH agent? Check out this “How to generate SSH key and add to SSH agent in Windows/Linux/MacOS” page from GitHub.

Step 2: Register Keys with Respective Platforms

Hop onto GitHub or GitLab, or whatever floats your boat, and slide those public keys (key_personal.pub and key_work.pub) into the SSH settings.

Step 3: Configure SSH for Different Accounts

Time to set up separate SSH config files for your personal and work shindigs:

touch $HOME/.ssh/config_personal $HOME/.ssh/config_work

Fill ’em up like this:

Content of the config_personal file:

					Host github.com
     HostName github.com
     Port 22
     User git
     IdentityFile ~/.ssh/key_personal

Content of the config_work file:

					Host github.com
     HostName github.com
     Port 22
     User git
     IdentityFile ~/.ssh/key_work

Don’t forget to don the cloak of correct file permissions:

					sudo chmod 600 ~/.ssh/config_personal
sudo chmod 600 ~/.ssh/config_work
Step 4: Setup Git Configuration

Create not one, not two, but three Git configuration files:

					touch $HOME/.gitconfig $HOME/.gitconfig_personal $HOME/.gitconfig_work


In your main .gitconfig, lead the path to personal and work configurations:

    path = ~/.gitconfig_personal
[includeIf "gitdir:/home/john/projects/work/"]
    path = ~/.gitconfig_work

Stick to absolute paths, skip the symlinks—they won’t work here.

Ah, and replace /home/john/projects/work/ with your own work directory, Sherlock.

In each specific config file, spill the beans on name, email and SSH config file paths:

Content of the .gitconfig_personal file:

    name = "John Doe"
    email = personal@mail.com
    sshCommand = ssh -F ~/.ssh/config_personal

Content of the .gitconfig_work file:

    name = "John Doe at work"
    email = work@mail.com
    sshCommand = ssh -F ~/.ssh/config_work
Step 5: Test Your Git-fu

Hopping around directories, verify if the correct Git configuration is in play. Go to any work project, example: 

/home/john/projects/work/simple_work_project and run

					git config --show-origin --get user.email

The output should be:

					file:/home/john/.gitconfig_work    work@mail.com

Go anywhere outside /home/john/projects/work/ and run the same command again, the output should be:

file:/home/john/.gitconfig_personal    personal@mail.com

If all’s well in the rodeo, you can git along just fine with your various repositories. Just stick to the trail, keep those work-related repositories corralled in the /work folder, and let the SSH keys do their magic automatically. Easy as pie!

Step 6: Enjoy Seamless Git Usage!

With all the cards on the table, you’re now primed for Git greatness across your various accounts. Git going and may your commits be swift and your merges be painless!

Muntasir Billah Munna

21 May, 2024