You can have git use a speĀ­ciļ¬c SSH key, per repo.

This is great if you have - or would like to use - difĀ­ferĀ­ent SSH keys for each clients. To limit exĀ­poĀ­sure, I try and use sepĀ­aĀ­rate GitHub/GitLab/BitBucket acĀ­count + SSH key for each client.

If one key is comĀ­proĀ­mised, none of your other work is exĀ­posed.

Show me the code

When in the deĀ­sired repo on your terĀ­miĀ­nal, conĀ­ļ¬gĀ­ure by:

git config core.sshCommand "ssh -i ~/.ssh/id_ecdsa_clientX"

In this case, youā€™re diĀ­rectly conĀ­ļ¬gĀ­urĀ­ing git.

What is git doĀ­ing unĀ­der the hood? It uses an SSH enĀ­viĀ­ronĀ­ment variĀ­able to exĀ­eĀ­cute comĀ­mands based on your conĀ­ļ¬g.

The abĀ­stracĀ­tion is great beĀ­cause:

  • You donā€™t need to mess with the enĀ­viĀ­ronĀ­ment variĀ­ables on your shell.
  • It is a set-it-and-forĀ­get-it conĀ­ļ¬gĀ­uĀ­raĀ­tion. You do not need to supĀ­ply the env variĀ­able to each git comĀ­mand.

Simple and elĀ­eĀ­gant.

Introduced in git 2.10

Please note this works on git 2.10 + ā€“- reĀ­leased in late 2016.

You should be covĀ­ered, but if you wish to check your curĀ­rently inĀ­stalled verĀ­sion of git, run:

git --version

You can ļ¬nd the 2.10.0 reĀ­lease notes here.

Quote for conĀ­veĀ­nience:

A new conĀ­ļ¬gĀ­uĀ­raĀ­tion variĀ­able core.sshĀ­ComĀ­mand has been added to specĀ­ify what value for GIT_SSH_COMMAND to use per reposĀ­iĀ­tory.