Stories

Discover boundless stories from unique narrators (storytellers 🙃)

How to use multiple github accounts from one device

admin1 • Mar 25, 2025

To manage and use two different GitHub accounts on your MacBook (M1 Air), you can configure your SSH keys and Git configurations for each account. Here's a step-by-step guide:

1. Generate Separate SSH Keys for Each Account

First, you need to generate SSH keys for both GitHub accounts.

For the 1st GitHub Account (00017501.wiut.student@gmail.com):

  1. Open your terminal and generate an SSH key:

    ssh-keygen -t rsa -b 4096 -C "00017501.wiut.student@gmail.com"
    
  2. When prompted to save the file, save it as:

    /Users/your-username/.ssh/id_rsa_00017501
    
  3. When asked for a passphrase, you can leave it empty or set one (optional).

For the 2nd GitHub Account (hamidaxtamov1@gmail.com):

  1. Generate another SSH key for the second account:

    ssh-keygen -t rsa -b 4096 -C "hamidaxtamov1@gmail.com"
    
  2. Save it as:

    /Users/your-username/.ssh/id_rsa_hakucodes
    
  3. Set a passphrase if needed (optional).

2. Add SSH Keys to SSH Agent

Once you've generated the keys, add them to the SSH agent.

  1. Start the SSH agent:

    eval "$(ssh-agent -s)"
    
  2. Add both keys to the SSH agent:

    For the 1st account:

    ssh-add ~/.ssh/id_rsa_00017501
    

    For the 2nd account:

    ssh-add ~/.ssh/id_rsa_hakucodes
    

3. Add SSH Keys to GitHub

You need to add the generated public SSH keys to their respective GitHub accounts.

  1. Copy the public key for each account:

    For the 1st account:

    pbcopy < ~/.ssh/id_rsa_00017501.pub
    

    For the 2nd account:

    pbcopy < ~/.ssh/id_rsa_hakucodes.pub
    
  2. Go to your respective GitHub profiles:

    • For the 1st account, navigate to Settings > SSH and GPG keys > New SSH key and paste the key.
    • Do the same for the 2nd account.

4. Configure SSH Config File for Multiple Accounts

Next, you need to configure your SSH config file so Git knows which SSH key to use for each account.

  1. Open the SSH config file in your editor:

    nano ~/.ssh/config
    
  2. Add the following configuration:

    # For 1st GitHub Account (00017501)
    Host github.com-00017501
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_00017501
    
    # For 2nd GitHub Account (hakucodes)
    Host github.com-hakucodes
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_hakucodes
    

    This tells Git which SSH key to use for each account.

5. Use Different GitHub Accounts in Different Repositories

Now, you can associate a specific GitHub account with each repository by configuring the git remote URL to match the correct SSH configuration.

For the 1st Account (00017501):

  1. Navigate to the repository where you want to use the first account:

    cd /path/to/your/repo
    
  2. Set the remote URL to use the 1st account:

    git remote set-url origin git@github.com-00017501:yourusername/yourrepo.git
    

For the 2nd Account (hakucodes):

  1. Navigate to the repository where you want to use the second account:

    cd /path/to/your/second-repo
    
  2. Set the remote URL to use the 2nd account:

    git remote set-url origin git@github.com-hakucodes:yourusername/yourrepo.git
    

6. Set Git User Config for Each Repository

To make sure commits are linked to the correct GitHub account, you need to set the user email and name for each repository.

For the 1st Account:

  1. Navigate to the 1st account's repository:

    cd /path/to/your/repo
    
  2. Set the username and email:

    git config user.name "00017501"
    git config user.email "00017501.wiut.student@gmail.com"
    

For the 2nd Account:

  1. Navigate to the 2nd account's repository:

    cd /path/to/your/second-repo
    
  2. Set the username and email:

    git config user.name "hakucodes"
    git config user.email "hamidaxtamov1@gmail.com"
    

7. Verify the Setup

To ensure everything is set up correctly, run the following commands in your repositories to check if the correct SSH key and user info are being used.

  • Check the remote URL:

    git remote -v
    
  • Check the commit user configuration:

    git config user.name
    git config user.email
    

Conclusion

By following these steps, you can seamlessly work with two different GitHub accounts on your MacBook. The SSH key setup will allow you to push and pull from repositories linked to either account, while the user configuration will ensure your commits are correctly attributed to the right GitHub account.


...