How to manage multiple git accounts (Ubuntu/ Mac) | by ByteCode Pandit

Hi, all teach worries, usually, we fall in a situation where we have to use both our personal as well as office GitHub account. So today I am gonna show you how can we manage multiple GitHub account on a machine. For now, I will set up two accounts.



Please follow the below steps.

Step 1: Have to create the ssh key for your accounts

$ cd ~/.ssh

$ ssh-keygen -t rsa -b 4096 -C "peronsal_email_id"

 # save as id_rsa_personal

$ ssh-keygen -t rsa -b 4096 -C "office_email_id"

 # save as id_rsa_office

Step 2: Have to add id_rsa_personal.pub and id_rsa_office.pub to their respective Github account.

Step 3: Create a config file in the .ssh folder and add the below configs

$ touch config

 # Personal account - default config

 Host github.com-personal

   HostName github.com

   User git

   IdentityFile ~/.ssh/id_rsa_personal

 # Work account

  Host github.com-office

   HostName github.com

   User git

   IdentityFile ~/.ssh/id_rsa_office

Step 4: Create .gitconfig for personal and office directory with respective config git host names

$ cd ~

$ nano ~/.gitconfig

  [user]

    name = personal_name

    email = personal_email_id

    [includeIf "gitdir:~/office/"]

    path = ~/office/.gitconfig

$ mkdir office

$ nano ~/office/.gitconfig

    [user]

      name = office name

      email = office_email_id

Step 5: Remove all existing keys

$ cd ~/.ssh

$ ssh-add -D

Step 6: Add new ssk keys

$ ssh-add id_rsa_personal

$ ssh-add id_rsa_office

   # check whether ssh added or not

$ ssh-add -l 

Step 7: Check configuration is right by pinging to GitHub with the below commands.

$ ssh -T github.com-personal 

$ ssh -T github.com-office


 #. Congratulations all done!

Step 8: Always clone repo by adding hostname in remote URL

e.g: git clone git@github.com-peronsal:bytecodepandit/repo_name.git

git clone git@github.com-office:bytecodepandit/repo_name.git








Comments

  1. Really helpfull , i faced this problem in past, after reading your blog i will surely not face this problem.
    Thanks Prakash

    ReplyDelete

Post a Comment

Popular posts from this blog

Real-Life Use-Cases for useCallback and useMemo

React's useImperativeHandle with practical example | by ByteCode Pandit

Understand NgModule in 15 mins | by Bytecode Pandit