DEV Community

loading...
Cover image for Easy way to manage multiple git accounts using ssh

Easy way to manage multiple git accounts using ssh

Shahid Ullah Khan
A javascript developer looking for new ways to create awesome solutions.
・2 min read

If you’re having problems managing different ssh keys for your different git accounts, This article is for you! follow below steps to do it easily.

Requirements: Nodejs, Npm

You will need to install and use npm package called ssh-keyman. (Click here to view docs)

Step 1 — Installation

Run below command to install ssh-keyman package.

$ npm i -g ssh-keyman
Enter fullscreen mode Exit fullscreen mode

Step 2 — Initialisation

Now initialise ssh-keyman using below command

$ ssh-keyman -i
Enter fullscreen mode Exit fullscreen mode

By running above command, this package will setup everything up in your system. It will create a default environment in your system & copy your current ~/.ssh/ content to default environment.

Now let’s assume we‘ve two different repositories from two different github profiles githubx and githuby.

Step 3 — Create environment for your account.

Now let’s create two environments, One for githubx and other for githuby.

$ ssh-keyman -c githubx
Enter fullscreen mode Exit fullscreen mode

Above command will create a profile called githubx in your system.

Create githubx environment

Similarly, Create another environment for our another github account githuby.

$ ssh-keyman -c githuby
Enter fullscreen mode Exit fullscreen mode

Create githuby environment

Now we’ve both environments setup for githubx and githuby.

Step 4 — Setup SSH Keys.

Let’s now create ssh key for your githubx account. For that we need to switch to githubx environment.

To switch to githubx environment run:

$ ssh-keyman -s githubx
Enter fullscreen mode Exit fullscreen mode

Switch githubx environment

Now we’re in githubx environment, Let’s generate ssh keys for githubx.

$ ssh-keygen -t rsa
Enter fullscreen mode Exit fullscreen mode

once the key is generated, use that key in your GitHub account, copy contents of ~/.ssh/id_rsa.pub and paste it in github’s ssh key section.

Github add key

Repeat the above process for githuby account.
I.e

  • Switch to githuby environment
$ ssh-keyman -s githuby
Enter fullscreen mode Exit fullscreen mode
  • Generate key for githuby environment.
$ ssh-keygen -t rsa
Enter fullscreen mode Exit fullscreen mode
  • Similarly login to githuby account in the browser and add the contents of ~/.ssh/id_rsa.pub to github create key’s key section.

Step 5 — Start using and switching.

So, If you want to work in repos from githubx account, simply switch to githubx environment using

$ ssh-keyman -s githubx
Enter fullscreen mode Exit fullscreen mode

Now when ever you push or pull the code from git, keys for githubx will be used.

Similarly if you want to work in repos from githuby account, simply switch to githuby environment using

$ ssh-keyman -s githuby
Enter fullscreen mode Exit fullscreen mode

And when ever you push or pull code from git, keys for githuby will be used.

Discussion (0)