Skip to content

Instantly share code, notes, and snippets.

@rahmatrhd
Created July 8, 2019 09:29
Show Gist options
  • Save rahmatrhd/84ef68056c94e5a2eca90599a3821b76 to your computer and use it in GitHub Desktop.
Save rahmatrhd/84ef68056c94e5a2eca90599a3821b76 to your computer and use it in GitHub Desktop.
Managing multiple git accounts

Copy and pasted from: https://mherman.org/blog/managing-multiple-github-accounts/

Managing Multiple Git Accounts

Let’s look at how to manage multiple Github accounts from one computer. In essence, it’s simply a matter of balancing both your git and ssh configurations - which actually is not as bad as it might seem.

Note: This tutorial is meant for Unix users.

Set up SSH Keys

Let’s assume your two Github accounts are named githubPersonal and githubWork, respectively.

Create two SSH keys, saving each to a separate file:

$ cd ~/.ssh
$ ssh-keygen -t rsa -C "your_email@associated_with_githubPersonal.com"
# save it as id_rsa_personal when prompted
$ ssh-keygen -t rsa -C "your_email@associated_with_githubWork.com"
# save it as id_rsa_work when prompted

The above commands setup the following files:

  • id_rsa_personal
  • id_rsa_personal.pub
  • id_rsa_work
  • id_rsa_work.pub

Add the keys to your Github accounts:

Copy the key to your clipboard:

$ pbcopy < ~/.ssh/id_rsa_personal.pub

Add the key yo your account:

  • Go to your Account Settings
  • Click “SSH Keys” then “Add SSH key”
  • Paste your key into the “Key” field and add a relevant title
  • Click “Add key” then enter your Github password to confirm

Repeat the process for your githubWork account.

Create a configuration file to manage the separate keys

Create a config file in ~/.ssh/

$ touch config

Edit the file using the text editor of your choice. I used vim - $ vim config:

# githubPersonal
Host personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal

# githubWork
Host work
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work

Update stored indentities

Clear currently stored indentities:

$ ssh-add -D

Add new keys:

$ ssh-add id_rsa_personal
$ ssh-add id_rsa_work

Test to make sure new keys are stored:

$ ssh-add -l

Test to make sure Github recognizes the keys:

$ ssh -T personal
Hi githubPersonal! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T work
Hi githubWork! You've successfully authenticated, but GitHub does not provide shell access.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment