Post

managing multiple git accounts with ssh keys

The problem

Recently, I needed to create a separate GitHub account for university. To streamline my workflow, I wanted to use SSH keys, just like with my existing account. However, I quickly discovered that Git has difficulty determining which SSH key to use when both accounts are on the same platform (GitHub in this case). After some research, I found that the most prominent solution was to edit the .ssh/config file and create a unique host configuration for each account. Similar to the example below.

1
2
3
4
5
6
7
8
9
Host github.com-personal
  User git
  HostName github.com
  IdentityFile ~/.ssh/<personal_ssh_key>

Host github.com-university
  User git
  HostName github.com
  IdentityFile ~/.ssh/<university_ssh_key>

Now, every time we clone a repository, we have to use a command like git clone git@github.com-personal:username/repository. This process is tedious because it requires editing the clone URL each time instead of simply running it in the terminal. Additionally, I usually sign my commits with my SSH key to ensure that others can verify the commits are genuinely from me and not from someone with unauthorized access to my account

The Solution

This is where Git profiles come to the rescue. By using conditional imports with includeIf based on the directory, Git profiles allow you to load a specific configuration into your main config file. Even better, you can override the SSH command to use a designated SSH key—exactly what I’ll be doing.

Step: 1 Create Your SSH keys

1
2
$ ssh-keygen -t ed25519 -C "personal@example.com"
$ ssh-keygen -t ed25519 -C "university@example.com"

Step: 2 Setting Up Your Git Config

Edit your .gitconfig file to something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Global settings go here
[gpg]
format = "ssh"
[gpg "ssh"]
program = "/Applications/1Password.app/Contents/MacOS/op-ssh-sign"
[commit]
gpgsign = true
[push]
autoSetupRemote = true
[user]
name = "your_name"

# Based on the directory we include our gitconfig
[includeIf "gitDir:~/Development/personal/"]
path = "~/.gitconfig.personal"
[includeIf "gitDir:~/Development/university/"]
path = "~/.gitconfig.university"

Make sure your gitDir path ends with a / otherwise your config will not load

Step 3: Create And Configure Your Git Profiles

1
2
3
4
5
6
[user]
email = "your_personal_email"
signingkey = "<your signing key"

[core]
sshCommand = "ssh -i ~/.ssh/<sshkey>"
1
2
3
4
5
6
[user]
email = "your_university_email"
signingkey = "<your signing key>"

[core]
sshCommand = "ssh -i ~/.ssh/<sshkey>"

If you’re using 1 password like I am to manage your ssh keys. Download your public key and point your command to use that instead.

Step 4: Verify Your Profiles

1
2
3
4
5
~> cd Development/personal
~/Development/personal> git config -l
user.name=your_name
user.email=your_personal_email
user.signingkey=your_signing_key

Your output should now hopefully contain something similar to those lines. Make sure that the user.email matches the email set for your profile, which should be your personal_email in this scenario.

Troubleshooting Tips

  • Verify your SSH setup by running ssh -i ~/.ssh/<ssh_key> git@<provider>.com (replace with GitHub, Bitbucket , etc.).
  • Ensure your gitDir ends with a slash, e.g., "gitDir:~/Development/<folder>/".
  • Double-check that you’re in the correct directory, such as /Development/personal for personal projects. If issues persist, feel free to leave a comment below.
This post is licensed under CC BY 4.0 by the author.