github multiple users

Problem

When you’re working with multiple GitHub accounts and repositories, managing different SSH keys can quickly become an impossible task. Perhaps you have a personal GitHub account and another one for work, and each one has different permissions and SSH keys. This can be especially tricky when trying to interact with remote repositories - cloning, pulling, and pushing - without getting tangled in authentication errors. As you switch between accounts, the question becomes: How do you efficiently manage multiple SSH keys for different GitHub accounts?

[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

Looks familiar?

Initial Solution: .ssh/config

One approach to this problem is to use the SSH config file, located in the ~/.ssh/ directory. By configuring SSH aliases in this file, you can associate different keys with different hosts, effectively directing SSH to use a specific key for each GitHub account.

Here is an example of how you can set up your ~/.ssh/config:

# Personal GitHub account
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal

# Work GitHub account
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_work

In this setup, github.com-personal and github.com-work are aliases that point to GitHub, each with a unique SSH key. When cloning or interacting with repositories, you’d use the alias corresponding to the right account. However, this method requires remembering to use the right host alias for each repository, which can get messy as the number of accounts and repositories grows.

There’s also an issue of dependencies. Often the URLs get mixed up when either NPM/Yarn or Go Vendor dependencies are at play. Attempted pulls from the configured aliases will not work outside of SSH.

Better Solution: .gitconfig

A cleaner solution lies in the utilization of Git’s powerful configuration system. Specifically, the .gitconfig file allows you to specify different users, keys, and settings for each workspace or directory.

You can set your global .gitconfig to include a different .gitconfig file based on the current directory. For example:

# ~/.gitconfig
[includeIf "gitdir:~/work/"]
  path = .gitconfig-work
[includeIf "gitdir:~/personal/"]
  path = .gitconfig-personal

Next, in each of these separate .gitconfig files, specify the user information for each account:

# ~/.gitconfig-work
[user]
  name = Work Name
  email = [email protected]

# ~/.gitconfig-personal
[user]
  name = Personal Name
  email = [email protected]

With this setup, Git will automatically use the correct user information based on the current directory, eliminating the need to remember to use the right alias. This makes managing multiple GitHub accounts much more scalable and efficient.