Skip to content

Instantly share code, notes, and snippets.

@yajra
Last active March 5, 2025 09:18
Show Gist options
  • Save yajra/59e7997a0e566b845643779a41393335 to your computer and use it in GitHub Desktop.
Save yajra/59e7997a0e566b845643779a41393335 to your computer and use it in GitHub Desktop.
Multiple Github Account Setup

Multiple Github Account Setup

GOAL: To setup work and personal github account in the same machine. The gist assumes that you already have the ssh key for work and personal use.

Solution 1: via ~/.ssh/config

Edit ~/.ssh/config and add an entry for your work and personal github account with different keys:

# Personal Account
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/personal_rsa
    IdentitiesOnly yes
    
 # Work Account
 Host work.github.com
    HostName  github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/work_rsa
    IdentitiesOnly yes

Cloning a personal repository

You can clone as usual:

git clone [email protected]:user/repo.git

Cloning a work repository

When cloning, you need to change the url to work.github.com

git clone [email protected]:user/repo.git

Solution 2: via ~/.gitconfig

Edit ~/.gitconfig and add a conditional config loaded based on working directory.

[includeIf "gitdir:/Users/yajra/work/"]
    path = ~/.gitconfig-work

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

Create 2 gitconfig files for work and personal setup

  1. ~/.gitconfig-work
[user]
  name = John Doe
  email = [email protected]

[core]
  sshCommand = ssh -i ~/.ssh/work_rsa
  1. ~/.gitconfig-personal
[user]
  name = John Doe
  email = [email protected]

[core]
  sshCommand = ssh -i ~/.ssh/personal_rsa

Cloning Repositories

  • All personal repositories should be cloned inside /Users/yajra/personal folder.
  • Work related repositories should be cloned inside /Users/yajra/work folder.

Using solution #2, committer name and email will adjust accordingly and there is no need to update the clone URL everytime we clone a work repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment