Setup Multiple Github Account

When you have multiple GitHub accounts and encounter issues with incorrect access (e.g., pushing/pulling from the wrong account), you can fix this by managing SSH keys and configuring Git for multiple accounts. Here's how:


1. Use Separate SSH Keys for Each GitHub Account

Step 1: Check Existing SSH Keys

Run this command to check if you already have SSH keys:

ls ~/.ssh/

Look for files like id_rsa and id_rsa.pub. If you already have a key for one account, you'll need a new key for the second account.

Step 2: Generate a New SSH Key

Generate a new SSH key for the second account:

ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
  • When prompted, save the key with a unique name (e.g., id_rsa_github):
    Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_github
    
  • Enter a passphrase (optional but recommended).

Step 3: Add the Key to the SSH Agent

Add the new SSH key to the SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_github

Step 4: Add the SSH Key to GitHub

Copy the public key:

cat ~/.ssh/id_rsa_github.pub

Then it to the corresponding GitHub account under Settings > SSH and GPG keys > New SSH key.

Step 5: Repeat for the second email

Repeat the 2nd to 4th process if you do not have existing connected ssh


2. Configure SSH for Multiple Accounts

Create or edit the SSH configuration file:

vi ~/.ssh/config

Add the following configuration for your accounts:

# Default account
Host github.com-default
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa
# Second account
Host github.com-second
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github

3. Clone Repositories Using Specific Accounts

When cloning a repository for the second account, use the custom Host name from your SSH config:

git clone git@github.com-second:username/repository.git

For the default account:

git clone git@github.com-default:username/repository.git

4. Set Up Git Config for Each Repository

Each repository can have its own Git user configuration. Navigate to the repository and set the local configuration:

cd /path/to/repo
git config user.name "Your Name"
git config user.email "your-email@example.com"

To verify:

git config --local --list

5. Test the Configuration

Test the SSH connection for each account:

ssh -T git@github.com-default
ssh -T git@github.com-second

You should see a success message like:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

7. Use HTTPS for One Account (Optional)

If managing SSH keys feels cumbersome, you can use SSH for one account and HTTPS for another:

  1. For the HTTPS account, set up a Personal Access Token (PAT) in GitHub. (https://github.com/settings/personal-access-tokens)
  2. Use the HTTPS URL for repositories of that account:
    git clone https://github.com/username/repository.git
  3. When prompted for credentials, use the PAT instead of the username and leave the password blank.

By setting up SSH keys and Git configuration properly, you can seamlessly work with multiple GitHub accounts!