Unlocking the Power of Multiple GitHub Accounts: Mastering Terminal Configuration

Unlocking the Power of Multiple GitHub Accounts: Mastering Terminal Configuration

Introduction:

Using multiple GitHub accounts is a common scenario for developers who work on personal and professional projects simultaneously. However, managing these accounts within your terminal can be a bit tricky. In this guide, I will walk you through the process of adding multiple GitHub accounts to your terminal and configuring them correctly, ensuring a seamless experience when working on different projects. So let's dive in and learn how to manage multiple GitHub accounts effortlessly.

Step 1: Generating SSH Keys

The first step is to generate SSH keys for each of your GitHub accounts. Open your terminal and navigate to the .ssh directory using the command

cd ~/.ssh

Once you're in the directory, use the command

ssh-keygen -t rsa -b 4096 -C "name"

to generate the SSH key. Make sure to provide a unique name for each key to differentiate between accounts.

Step 2: Configuring SSH Keys

  • After generating the SSH keys, you will find 2 files for each key in the .ssh directory. Now it's time to set up the SSH keys with the appropriate GitHub accounts. Copy the public key (.pub) and add it to your GitHub account in browser.

  • Create a config file in the .ssh directory using the command

      $ touch ~/.ssh/config
    
  • Open the config file and add the following configurations for each account:

      Host github.com-personal
           HostName github.com
           User git
           IdentityFile ~/.ssh/id_rsa_personal
      Host github.com-work
           HostName github.com
           User git
           IdentityFile ~/.ssh/id_rsa_work
    

Step 3: Configuring Git

  • Next, navigate to your home directory using the command cd and create a .gitconfig file using touch .gitconfig. Open the .gitconfig file and add the following configuration:

      [user]
          name = <name>
          email = <email>
    
      [includeIf "gitdir:~/work/"]
          path = ~/.gitconfig-work
    

    Make sure to replace <name> and <email> with your personal information.

  • Also create .gitconfig-work with touch ~/.gitconfig-work and write it with the following configuration

      [user]
          name = <name>
          email = <email>
    

    Make sure to replace <name> and <email> with your work information.

Step 4: Adding SSH Keys

To add the SSH keys to your terminal, navigate to the .ssh directory using the command cd .ssh. Once inside the directory, clear any existing identities using ssh-add -D. Then, add one identity at a time using the following commands:

ssh-add id_rsa_personal
ssh-add id_rsa_work

You can verify the added identities using ssh-add -l.

Step 5: Testing SSH Connection

To ensure that the SSH keys are set up correctly, you can test the connection for each GitHub account. Use the command ssh -T github.com-<name> for each account, replacing <name> with the appropriate account name. This will verify if the SSH key is correctly associated with the respective GitHub account.

Step 6: Cloning Repositories

When cloning a repository, make sure to use the SSH link and specify the GitHub account name. For example:

git clone git@github.com-<name>:<username>/<repo>.git

Replace <name> with the appropriate account name, <username> with your GitHub username, and <repo> with the repository name.

Step 7: Verification

To verify that the configuration is correct, use the command git config user.name. This will display the configured username for the repository.

Conclusion:

Congratulations! You've successfully added multiple GitHub accounts to your terminal and configured them to work seamlessly. With this newfound knowledge, you can easily manage and switch between your personal and work GitHub accounts, enhancing your productivity and organization. Now you can focus on your projects without worrying about mixing up repositories or committing with the wrong identity.

Did you find this article valuable?

Support Chaitanya Shahare by becoming a sponsor. Any amount is appreciated!