How to manage multiple Github accounts
For the developer who is dealing with more than one Git account.
The following steps will help with carefully and efficiently managing multiple Git accounts. Simply copy and paste the commands onto your terminal and check the code from your repository.
Step 1. Check for existing SSH keys
- Open Terminal app.
- Enter
ls -al ~/.ssh
to see if existing SSH keys are present. List the files in your .ssh directory, if they exist. - Check the directory listing to see if you already have a public SSH key.
- By default, the filenames of public keys are one of the following:
id_dsa.pub
id_ecdsa.pub
id_ed25519.pub
id_rsa.pub
Tip: If you don’t have an existing public and private key pair, or prefer not to use any that are available to connect to GitHub, you can generate a new SSH key.
Step 2. Create new SSH
Ed25519 algorithm:
ssh-keygen -t ed25519 -C "workEmail@example.com "
or
ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519 -C "workEmail@example.com"RSA algorithm:
ssh-keygen -t rsa -b 4096 -C "personalEmail@gmail.com"
ssh-keygen -t rsa -b 4096 -C "workEmail@work.com"
ssh-keygen -t rsa -b 4096 -C "freelanceEmail@work2.com"
Step 3. Add your private SSH key to the ssh-agent and store your passphrase in the keychain
ssh-add --apple-use-keychain ~/.ssh/id_rsa
or
ssh-add -K ~/.ssh/id_rsa
Step 4. See the list of added SSH
ssh-add -l
This will list the fingerprints of all identities.
Step 5. Add a new SSH key to your account
$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
Tip: If pbcopy isn’t working, you can locate the hidden .ssh folder by opening the file in your favorite text editor and copying it to your clipboard.
- Go to your Github account.
- In the upper-right corner of any page, click your profile photo, then click Settings.
3. In the user settings sidebar, click SSH and GPG keys.
4. Click New SSH key or Add SSH key.
5. In the Title field, add a descriptive label for the new key.
Paste your key into the Key field.
6. Click Add SSH key.
7. Follow the same steps to copy id_rsa_work and id_rsa_work2 key onto your work and freelance accounts.
Step 6. Make changes in the config file
- Go to .ssh folder.
- Open config file in any textEditor.
- If config file is not present, create one.
cd ~/.ssh
touch config
Step 7. Copy and paste this text into the config file
# Personal GitHub account
Host github.com
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa# Work VSTS account
Host vs-ssh.visualstudio.com-work
HostName vs-ssh.visualstudio.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa_work# Work2 GitHub account
Host github.com-work2
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa_work2
In my case, my personal and freelance accounts are on GitHub, and my work account is on VSTS.
Step 8. Clone Repo (Freelance Github Account)
Copy the clone URL from Github (from work2 account) and modify as below.
Original url:
git clone git@github.com:<Repo path>.git
Modified url:
git clone git@github.com-work2:<Repo path>.git
Paste this (modified) URL onto the terminal.
Step 9. Configure User attributes
Go into the repository folder from terminal and paste this command.
git config user.name "work2"
git config user.email "freelanceEmail@work2.com"
Step 10. Repeat steps 8 and 9 for other (personal and work) accounts
Cheers 🥂
Extra Commands for HELP
- Delete all identities.
ssh-add -D
2. For more help, use the following commands.
ssh -help
ssh-add -help
P.S : We’re hiring at Heady! 👋 If you would like to come work with us on some really cool apps please check out our careers page.