I wanted to hook into this topic, because I have recently been trying to solve this without having to do a git config core.sshCommand
every new release I clone
So, for that I extended a bit on the implementation from Jakuje
I have two gitlab accounts. One is for work, with my work email, and one is personal, with my personal email.
For this I have two different private keys:
- personal:
~/.ssh/keys/personal.id_rsa
- work:
~/.ssh/keys/work.id_rsa
Now, I have a global git configuration located at ~/.gitconfig
and an alternative configuration located at ~/work.gitconfig
. The work git configurations only contains settings that are different than the global configuration.
So in this scenario,
~/work.gitconfig
[core]
sshCommand = ssh -i ~/.ssh/keys/work.id_rsa
and all my work related repositories I clone into ~/source/work
The magic then happens in my ~/.gitconfig
. I included the following section (git includeIf
:
[includeIf "gitdir:~/source/work/**"]
path="~/work.gitconfig"
Now, this will tell git
to include my work git configuration for all repos that are in ~/source/work
. This configuration will then automagically set the ssh key used when pushing/pulling to the one I use for my work repos.
All you need to make sure is to clone into the right folder.
You can combine this with using a generic ssh config.
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/keys/personal.id_rsa
By default git will use the personal.id_rsa
(personal account), except when the repo is in the work
directory, then the work account is used (based on ssh key).