Managing Multiple Github Accounts

itismowgli

Parth

Posted on September 23, 2022

Managing Multiple Github Accounts

Let’s see how many GitHub accounts can be managed from a single computer. In essence, it is a matter of balancing your git and ssh configurations, which is not as difficult as it may appear.

This guide is intended for Unix users.

Set up SSH Keys
On your local workstation, you can create a new SSH key. After generating the key, you can add it to your GitHub account to allow SSH authentication for Git activities.

Note: On March 15, 2022, GitHub increased security by removing obsolete, unsafe key types.
DSA keys (ssh-dss) are no longer supported as of that date. You cannot add additional DSA keys to your own GitHub account.
RSA keys (ssh-rsa) with a valid_after date prior to November 2, 2021 may continue to employ any signature algorithm. After that date, RSA keys must be produced using the SHA-2 signature technique. Some older clients may require an upgrade to utilise SHA-2 signatures.

Assume that your two Github accounts are named githubPersonal and githubWork.

Create two SSH keys and store them in different files:

$ cd ~/.ssh
$ ssh-keygen -t rsa -C "personal@email.com"
# save it as id_rsa_personal when prompted
$ ssh-keygen -t rsa -C "work@email.com"
# save it as id_rsa_work when prompted
Enter fullscreen mode Exit fullscreen mode

With the above commands, the following files are created:

id_rsa_personal
id_rsa_personal.pub
id_rsa_work
id_rsa_work.pub
Enter fullscreen mode Exit fullscreen mode

Adding a new SSH key to your GitHub Personal account
Copy the SSH public key to your clipboard.

If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.

$ pbcopy < ~/.ssh/id_rsa_personal.pub
# Copies the contents of the id_rsa_personal.pub file to your clipboard
Enter fullscreen mode Exit fullscreen mode

Tip: If pbcopy isn't working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

Add the key to 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 GitHub work account.

Adding your SSH key to the ssh-agent
If you’re using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain.

  • First, check to see if your ~/.ssh/config file exists in the default location.
$ open ~/.ssh/config
> The file /Users/you/.ssh/config does not exist.
Enter fullscreen mode Exit fullscreen mode
  • If the file doesn’t exist, create the file.
    $ touch ~/.ssh/config

  • Open your ~/.ssh/config file, then modify the file to contain the following lines. If the name or path of your SSH key file is different from the example code, change the filename or path to match your setup.

# githubPersonal
Host personal
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_personal
# githubWork
Host work
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_work
Enter fullscreen mode Exit fullscreen mode

Update stored identities
Clear currently stored identities:

$ ssh-add -D

Add new keys:

$ ssh-add -K ~/.ssh/id_rsa_personal
$ ssh-add -K ~/.ssh/id_rsa_work
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Make default account global

git config --global user.name "personal"
git config --global user.email "personal@email.com"
Enter fullscreen mode Exit fullscreen mode

Test PUSH
Make a new repository called “test-personal” in your personal account on Github.

Back on your local machine, create a test directory:

$ cd ~/documents
$ mkdir test-personal
$ cd test-personal
Enter fullscreen mode Exit fullscreen mode

Add a blank readme.md file to Github and push:

$ touch readme.md
$ git init
$ git add .
$ git commit -am "first commit"
$ git remote add origin git@personal:githubPersonal/test-personal.git
$ git push origin master
Enter fullscreen mode Exit fullscreen mode

Notice how we’re using the custom account, git@personal, instead of git@github.com.

Repeat the process for your githubWork account.

Test PULL
Add some text to the readme.md file in your personal account on Github.

Now Pull and merge the changes by running the following command within the test-personal directory:

$ git pull origin master
Enter fullscreen mode Exit fullscreen mode

Again, repeat this for your githubWork account.

Clone a repository

$ git clone git@personal:githubPersonal/test-personal.git
# For personal repository
$ git clone git@work:githubWork/test-personal.git
# For Work repository
Enter fullscreen mode Exit fullscreen mode

Make sure you add local config to the project folder if you’re using a non-default(githubWork) account.

$ git clone git@work:githubWork/test-personal.git
$ git config user.name "work"
$ git config user.email "work@email.com"
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
itismowgli
Parth

Posted on September 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Managing Multiple Github Accounts
github Managing Multiple Github Accounts

September 23, 2022