Sign in
Log inSign up

How to setup and manage multiple Git accounts on a local machine

Using multiple GitHub accounts and organising multiple git profiles locally

Pranay Velisoju's photo
Pranay Velisoju
·Jan 23, 2022·

5 min read

How to setup and manage multiple Git accounts on a local machine

Photo by Alp Duran on Unsplash

TL;DR — This article is a practical guide on setting up and using multiple ssh keys in your local machine, each associated to a different Git account and its own git configuration.

You might have a hard time setting up and managing multiple GitHub accounts, Gitlab, Bitbucket or AWS Codecommit accounts from a single local machine. This is especially true if you are a freelancer or someone who recently joined a new organisation and are required to use a separate work Git account.

This practical guide will help you configure your *nix system to connect to multiple git account with ssh key based access.

Requirements

  • A Unix based machine
  • Basic knowledge of using terminal
  • Two active accounts of GitHub or BitBucket or Gitlab etc.

    Ex: personal linked using and work linked using

Check for existing keys

  1. Open Terminal.

    Enter ls -al ~/.ssh to see if existing SSH keys are present.

     $ ls -al ~/.ssh
     # Lists the files in your .ssh directory, if they 
     exist
    
  2. Check the directory listing to see if you already have a public SSH key. By default, the filenames of supported public keys for GitHub are one of the following.
  3. Either use an existing public key for one account or generate a new public key. In my case I am using the existing id_ed25519.pub for personal account and generating a new public key for work account.

Generate a new SSH Key

In case you don't have any ssh keys, generate new ssh keys for both personal and work accounts.

Generate work associated ssh key

Open terminal and paste the text below, substituting in your with any of your cloud repository hosting service linked email address.
$ ssh-keygen -t ed25519 -C ""

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/pranay/.ssh/id_ed25519):

Here enter the filename with the your preferred suffix ex: /home/pranay/.ssh/id_ed25519-org1

Generate personal ssh key

Repeat the step above if you don't already have one or want to have a separate key with personal suffix: id_ed25519-personal. In mycase I will be using the existing ssh key id_ed25519.

Add these keys to ssh agent

  • Check if the ssh agent is running in the background, if not start using Start the ssh-agent in the background.
    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
    
  • Before adding ssh keys, you need to configure your ssh by default it is located at ~/.ssh/config, if this file don't exist create one touch ~/.ssh/config

    Paste this content into the file and change accordingly.

Host github-personal
  HostName github.com
  ServerAliveInterval 60
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519

Host github-org1
  HostName github.com
  ServerAliveInterval 60
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519-outlier
  • Now add individual keys
$ ssh-add -K ~/.ssh/id_ed25519
$ sh-add -K ~/.ssh/id_ed25519-org1

Add these Public keys to GitHub

Now add the SSHs key to the corresponding GitHub, Gitlab, AWS Codecommit and other Git accounts. Copy the .pub file content and upload to Git accounts.

For Github:

  • In the upper-right corner of any page, click your profile photo, then click Settings.
  • In the user settings sidebar, click SSH and GPG keys. Click New SSH key or Add SSH key.
  • In the “Title” field, add a descriptive label for the new key. - For example, if you’re using a personal Mac, you might call this key “personal-thinkpad”.
  • Paste your key into the “Key” field.
  • Click Add SSH key.

Testing

Now lets test to see if we can access these individual Github accounts from the terminal


$ ssh -T git@github-personal 
Hi pranay-personal! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@github-org1
Hi pranay-org1! You've successfully authenticated, but GitHub does not provide shell access.

Now clone the private/public repositories using their respective accounts.

git clone ssh://github-personal/user1/private-repo.git

Configure multipe local git profiles

Before we can actually start cloning and pushing changes, we want to use respective git profiles.

Let's get straight to the solution – The answer lies in the .gitconfig file. This is starter point for Git to identify what configurations need to be used.

The idea is to segregate the repos on your machine into multiple directories by separating the profiles you want, and then define a .gitconfig file per profile.

Here I how I organized my projects.

  • ~/personal -> contains personal repos.
  • ~/org/org1 -> contains all projects related to organisation 1.
  • ~/org/org2 -> contains all projects related to organisation 2.

create a global Git configuration

Create the global .gitconfig file in your home directory if it doesn't already exist. Then add all the profile directories as an entry like in the example below.

The way this works is very intuitive – if the directory path where you created the Git directory matches one of the paths in includeIF, then Git uses that particular profile configuration file. Otherwise, it uses the default configuration.

Copy the below content at the top of this file ~/.gitconfig

[includeIf "gitdir:~/org/out1/"]
        path=~/org/out1/.gitconfig

[includeIf "gitdir:~/personal/"]
        path=~/personal/.gitconfig

This way you can create organisation specific profiles and also have your personal profile.

Create individual git profiles.

We just mentioned the ~/org/out1/.gitconfig and ~/personal/.gitconfig files in the global .gitconfig file, but we didn't create them yet. These individual files can contain all the customisation that you need, from user name and email to commit hooks.

~/org/org1/.gitconfig

[user]
 name = work_user_org1
 email = 

~/personal/.gitconfig

[user]
 name = personal_user
 email = personal_email

Lets verify

We're all set!
Check the configuration

$ cd ~/org/org1
$ mkdir work-test-repo
$ cd work-test-repo
$ git init
  *Initialized empty Git repository in /Users/dbarochiya/work/work-test-repo/.git/*
$ git config -l 

includeif.gitdir:~/personal/.path=~/personal/.gitconfig

 includeif.gitdir:~/org/org1/.path=~/org/org1/.gitconfig
        **user.name=work_user_org1
        user.email = **

Usage

Now the final step we all are waiting for, cloning the repository and start working.

Two things to keep in mind.

  • To use local git profile simply cd into respective directory and
  • To use a particular git account (work or personal github accounts) use its respective git domain.

clone repo:

Copy the clone URL from Github (from org1 work account) and modify as below.

Original url:

git clone git@github.com:(Repo path).git

Modified url:

git clone git@github-org1:(Repo path).git

Paste this (modified) URL onto the terminal.

In this case, you have cloned the project using your work git configuration. The same method can be used for your work-related projects. Bear in mind that all work projects need to be in their org directory for the work git configuration you have set up to work correctly.

Cheers. 🥂🥂