Follow the below steps to work with multiple accounts in GitHub.

1. Generating SSH Key

Run the below command to generate a new SSH key for the account that you want to add.

ssh-keygen -t ed25519 -C "your_email@example.com"

When prompted for “Enter file in which to save the key”, change the name to identify this particular account. Suffix personal is being used for this example.

Enter file in which to save the key (/Users/dbh/.ssh/id_ed25519):/Users/dbh/.ssh/id_ed25519_personal

Once you run the above command. You will be prompted with “Enter passphrase (empty for no passphrase)” – You can add a passphrase or you can just continue without entering any passphrase. Your key will be saved in the default location mentioned above.

Print the content of the saved public key by running the below command.

cat ~/.ssh/id_ed25519_personal.pub

This key now needs to be added to your GitHub account. Copy the key value and open you GitHub account in the browser.

2. Adding SSH key to GitHub

In the GitHub page – click on the profile avatar on top right corner and open Settings.
From Settings page select SSH and GPG keys option, under SSH keys section click on New SSH key. Provide a suitable title and paste the key in the Key section.

3. Adding SSH key to SSH-agent

Run the below command to tell SSH agent (your machine) about the new key and you should receive response that the identity is added.

ssh-add ~/.ssh/id_ed25519_personal

4. Creating config file

In the .ssh directory create a config file by running the following command (edit the config file if it already exists). This is required to let ssh know about these multiple keys.

touch ~/.ssh/config

Add the following lines:

#Default account 
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519

Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal

5. Updating gitconfig file to use different accounts

Now that the keys are added to config file, we need a way to specify GitHub about the different username and email used for Git operations.
For this, we need to update the ~/.gitconfig file to specify these details. Edit the ~/.gitconfig file and update the file like so (default details will already be present we need to add the second part).

[user]
    name = Default Name
    email = defaultemail@example.com

[includeIf "gitdir:~/<personal git projects directory>/**"]
    path = ~/personal/.gitconfig

In the includeIf section gitdir refers to the directory location where the personal account will be used for Git operations. For example, I used ~/Desktop/personal/projects/** to map all the repos under projects directory to use git config from the ~/personal/.gitconfig file.

Now, you need to create the ~/personal/.gitconfig file and specify secondary account details like below.

[user] 
    name = dbh
    email = your_email@example.com

6. Using the secondary account

You can now clone the repository using the newly added secondary account.

git clone git@github.com-personal:dbh/repo_name.git

Also, before pushing the changes make sure the add the remote repository . You can run the below command to set the remote url (In most cases this will be set and you will see an error message saying error: remote origin already exists).

git remote add origin git@github.com-personal:dbh/repo_name.git

Additionally, the below commands should output the secondary username and email that is added in the ~/personal/.gitconfig

git config user.name
git config user.email

All the repositories specified under gitdir (In this case, ~/Desktop/personal/projects/) will use the secondary account that is setup for all the Git operations.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights