Using different SSH identity files depending on Github projects

Daisuke Maki
Apr 1 · 2 min read

If you are in a situation like mine, you have personal projects on Github, but you are also part of corporate team which has private repositories, and they require you to go through single sign-on.

When you go through SAML SSO, you basically need to give the SSH key you are using SSO capabilities.

You could do this with the SSH key that you are already using for your personal projects, which is totally fine, but I really would like to keep my work and non-work credentials separate.

I mean, I really don’t want to screw up, so I’d like to play it safe.

So then I basically have to differentiate between these two:

git clone git@github.com:work-project/work-repository.git
git clone git@github.com:lestrrat/personal-repository.git

and provide separate identities depending on the path.

Initially I thought that this would be simple enough using .ssh/config and/or .git/config but it turned out harder than I thought.

After a few attempts, I came up with this: Use smartcd (or its alternatives like direnv) in the non-default project, which in my case is work-project, and just specify this environment variable.

GIT_SSH_COMMAND="ssh -i ~/.ssh/your_work_identify_file"

And that’s it! These tools automatically sets up different environments depending on the directory that are currently working on, and in git’s case it works out perfectly.

I use pecoto switch directories, and it works like a charm.

Daisuke Maki

Written by

Go/perl hacker; author of peco; works @ HDE Inc; mastermind of builderscon; Proud father of three boys;