Skip to content

Instantly share code, notes, and snippets.

@rcoedo
Last active September 27, 2022 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcoedo/b94665690b7ae2089798117a93636756 to your computer and use it in GitHub Desktop.
Save rcoedo/b94665690b7ae2089798117a93636756 to your computer and use it in GitHub Desktop.

Handling Multiple Github Accounts on MacOS

After many attempts with different approaches, the best solution I've found is to use a combination of the [includeIf] git config option and using ssh.

1. Generate the keys for every identity

> ssh-keygen -t ed25519 -C "your@email.com" -f ~/.ssh/id_<your-identity>

2. Add the identities to the SSH agent

> ssh-add --apple-use-keychain ~/.ssh/id_<your-identity>

3. Create a git config file for every identity

Create a .gitconfig.<your-identity> file and add the git config overrides for that specific identity. You should have, at least, the following keys:

[user]
  name = Your Name
  email = Your Email
[core]
  sshCommand = ssh -i ~/.ssh/id_<your-identity>

4. Add the overrides for the proper repositories or subfolders

At the end of your global .gitconfig file, add something like this:

[includeIf "gitdir:~/Workspace/<your-identity>/"]
path = ~/.gitconfig.<your-identity>

Make sure git is loading your configuration properly by going into the repositories and running git config user.email.

5. Make sure your ~/.ssh/config file is using Apple's keychain

The ssh config file should contain the following:

Host *
    UseKeychain yes
    AddKeysToAgent yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment