Skip to content

Instantly share code, notes, and snippets.

@gerlie-reyes
Last active December 14, 2022 01:55
Show Gist options
  • Save gerlie-reyes/8e61233641b299b0307508e706a30023 to your computer and use it in GitHub Desktop.
Save gerlie-reyes/8e61233641b299b0307508e706a30023 to your computer and use it in GitHub Desktop.

Setting up multiple Github accounts in one machine

When you are working in separate accounts, 1 for work-related and 1 for personal, you might encounter permission denied error, for example, saying that your work account is not allowed to push commit to your personal repo. On this config, I am using default github.com for work and then github.com-personal for personal account.

Create 2 Separate SSH Keys

$ cd ~/.ssh
$ ssh-keygen -t rsa -b 4096 -C "your_personal_email@domain.com"
  # save as id_rsa_personal
$ ssh-keygen -t rsa -b 4096 -C "your_work_email@domain.com"
  # save as id_rsa_work

Listing .ssh folder will give you 4 files

id_rsa_personal
id_rsa_personal.pub
id_rsa_work
id_rsa_work.pub

Copy the keys you created:

pbcopy < ~/.ssh/id_rsa_personal.pub

If pbcopy is not installed, you can use cat ~/.ssh/id_rsa_personal.pub then copy contents.

Add Keys to github account:

Go to github.com and click on your profile picture. then:

Settings > SSH and GPG keys > New SSH key > Paste key > Add SSH key

Create .ssh/config

Paste the following:

# Personal account - default config
Host github.com-personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal
# Work account
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work

Edit gitconfig.

I use vim to edit in CLI, if vim is not installed, you can use nano

vim ~/.gitconfig

Content:

[user]
    name = Gerlie Reyes
    email = gerlie.reyes@work.com
[includeIf "gitdir:~/personal/"]
    path = ~/personal/.gitconfig
    

Edit personal gitconfig

vim ~/personal/.gitconfig

Content:

[user]
   email = mendozagerlie@gmail.com

Save key identities in local machine

cd ~
ssh-add -D

On Windows cmder, you might encounter error: 'eval' command is not recognized. Make sure to select bash command in cmder + button.

Choosing bash in cmder

Add identities

ssh-add id_rsa_personal
ssh-add id_rsa_work

When adding remote origin for personal projects, example:

git remote add origin git@github.com-personal:gerlie-reyes/learn-react.git

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