Useful Notes
Table of contents
Use multiple GitHub accounts with SSH
First navigate to ~/.ssh
.
For organization, create a directory and name it github
. All private and public keys for GitHub connection will be placed here.
Generate a new SSH key
Follow the ssh-keygen
prompt. It will ask you to decide on a name for the file, passphrase, etc.
# If Ed25519 algorithm is supported
ssh-keygen -t ed25519 -C "your_github@email.com"
# Legacy RSA
ssh-keygen -t rsa -b 4096 -C "your_github@email.com"
Add the new SSH key to GitHub account
Easiest part.
Refer to GitHub documentation for step-by-step screencaps.
Modify config
Suppose I have two GitHub accounts each associated with personal_email@address.com
and work_email@address.com
.
I’ll assume the private keys are named github-personal
and github-work
respectively.
Now append the following to ~/.ssh/config
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/github/github-personal
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/github/github-work
You can define custom host for both as such or have one of them keep the default github.com
.
Local config per repository
First start a local repo with
git init
Then config local name and email that will be used for that repo
git config --local user.name "work_name"
git config --local user.email "work_email@address.com"
Add remote
Normally you would add an ssh remote by
git remote add origin git@github.com:github_username:repo_name
# OR
git remote add origin github.com:github_username:repo_name
But this time,
git remote add origin github-work:work_username:repo_name
References: