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.


        name = Benedict Roeser
        email =

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


        name = broe123
        email = broe@xyz.example

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

I hope this small hint helps some of you.

Leave a Reply