OGIOS

personal website.


Table of contents

Manage multiple accounts bind with different ssh keys on same machine

It's been kind of annoying when you have to manage different Github accounts on the same pc.

Here's a way to do it.

How

  1. create different keys for different accounts, you can use -f "name" to set key filename.

  2. add these to your .ssh/config:

    Host github.com-ogios  # actually, `Host` is just an alias, it can be anything.
      Hostname ssh.github.com
      User git
      Port 443
      IdentityFile ~/.ssh/<private_key_file>
    
  3. add public keys to your accounts.

  4. remember you do everything with github.com-<name>, and your are good to go.

But it's restricted, you set a host alias for your account, which means every ssh operation is related to the Host, for example, your origin url will be look like this after you cloned a repo: git@github.com-<name>/**/**.git, if your are using submodule, you have to set that host alias to your .gitmodule which may cause some trouble.

This is not convenient for everyone, others may be confused after they have that repo cloned, and they will fail on initializing the submodules.

But it's a problem that can be fixed by adding something in your ~/.gitconfig file:

[url "git@github.com-<name>:<name>/"]
    insteadOf = git@github.com:<name>/

Now you don't have to use github.com-<name> every where, back to the normal usage, git will recognize and replace the url for you.

Another way

Simply add 2 lines into the .gitconfig file under the project folder of yours:

[core]
sshCommand = ssh -i ~/.ssh/<private_key> -F /dev/null

But this is also restricted because now you can't clone your repo directly but init a local repo and then add remote origin.

Inspired from: https://gist.github.com/rahularity/86da20fe3858e6b311de068201d279e3