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
.
> ssh-keygen -t ed25519 -C "your@email.com" -f ~/.ssh/id_<your-identity>
> ssh-add --apple-use-keychain ~/.ssh/id_<your-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>
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
.
The ssh config file should contain the following:
Host *
UseKeychain yes
AddKeysToAgent yes