Skip to content

git: Different ssh key per work directory

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.

~/.gitconfig

[user]
        name = Benedict Roeser
        email = mail@benedictroeser.de

[includeIf "gitdir:~/work/xyz/"]
        path = ~/work/xyz/.gitconfig

~/work/xyz/.gitconfig

[user]
        name = broe123
        email = broe@xyz.example

[core]
    sshCommand = "/usr/bin/ssh -i /home/benedict/.ssh/xyz_corp_id_rsa"

I hope this small hint helps some of you.

Leave a Reply