8

Easy way to manage multiple git accounts using ssh

 2 years ago
source link: https://dev.to/shahidullahkhankhattak/easy-way-manage-multiple-git-accounts-using-ssh-41hk
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 modeExit fullscreen mode

Step 2 — Initialisation

Now initialise ssh-keyman using below command

$ ssh-keyman -i
Enter fullscreen modeExit 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 modeExit fullscreen mode

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

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

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

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 modeExit fullscreen mode

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

$ ssh-keygen -t rsa
Enter fullscreen modeExit 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.

Repeat the above process for githuby account.
I.e

  1. Switch to githuby environment
$ ssh-keyman -s githuby
Enter fullscreen modeExit fullscreen mode
  1. Generate key for githuby environment.
$ ssh-keygen -t rsa
Enter fullscreen modeExit fullscreen mode
  1. 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 modeExit 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 modeExit fullscreen mode

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK