Assume you work on git projects for Xyz Corp in the folder /home/benedict/work/xyz and for Mega Company Inc in the folder /home/benedict/work/mega.
You have a different ssh key for each of these. Of course you don’t want to specify the key on every git command.
While there are a multitude of solutions, for me a combination of conditional includes and the sshCommand setting in .gitconfig works best. One benefit of the solution I selected is, that the correct SSH key is also used when cloning a new repository.
[user] name = Benedict Roeser email = firstname.lastname@example.org [includeIf "gitdir:~/work/xyz/"] path = ~/work/xyz/.gitconfig
[user] name = broe123 email = email@example.com [core] sshCommand = "/usr/bin/ssh -i /home/benedict/.ssh/xyz_corp_id_rsa"
I hope this small hint helps some of you.