how-to-manage-multiple-github-or-gitlab-account

Manage Multiple GitHub/GitLab Account

Static Badge Static Badge Static Badge

How to manage multiple GitHub or GitLab accounts

Published on July 20, 2024

We can communicate GitHub and GitLab Repository from our local machine by HTTPS and SSH. Suppose you have multiple GitHub or GitLab accounts and need all accounts to push and pull projects. Then it is boring to type username and password in every time. The best option is to add an SSH Key for each account then you don’t need to type a username and password for every single push and pull. I am going to show the process of how to create and add an SSH key in this article.

GitHub

I am going to assume you have two user ID username1 and username2 which are registered with EmailID username1@example.com and username2@example.com in GitHub respectively.

Step -1 : Creating an SSH key for username1:

    ssh-keygen -t rsa -b 4096 -C "username1@example.com"

During creation of ssh key asked to location/filename and type a passphrase.Which will create two file one with filename and other with .pub extension.

To see the created file in .ssh folder if you have choose to store in there ls -a ~/.ssh/ .

Step - 2: Start the SSH and adding the generated Key:

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

Step - 3: Adding SSH Key in GitHub:

Add SSH in GitHub

Step 4 : Verify the SSH Connection:

    ssh -T git@github.com

If everything is going perfect then you will see

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

Now you are ready to communicate GitHub via SSH for usernam1 account.

The content of config file :

# GitHub Username1 Account
Host github-username1
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_username1

# GitHub Username2 Account
Host github-username2
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_username2
git remote -v

GitLab

Follow similar steps as above to set up SSH keys for GitLab accounts. The documentation for GitLab SSH keys can be found here.

Step 1: Creating an SSH key for username1

ssh-keygen -t rsa -b 4096 -C "username1@example.com"

Step 2: Start the SSH agent and add the generated key

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

Step 3: Adding the SSH Key to GitLab

Add SSH key in GitLab

Step 4: Verify the SSH Connection

ssh -T git@gitlab.com

If everything is correct, you will see a message like:

Welcome to GitLab, @username1!

Step 5: Configuring Multiple SSH Keys

Create a config file at ~/.ssh/config with the following content:

# GitLab Username1 Account
Host gitlab.com
    HostName gitlab-username1
    User git
    IdentityFile ~/.ssh/id_rsa_username1

# GitLab Username2 Account
Host gitlab-username2
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_rsa_username2

Step 6: Using the Configured SSH Keys

To clone a repository:

git clone git@gitlab.com:username/repository.git

To push from the local repository to the web repository:

echo "# Repository Name" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@gitlab.com:username/repository.git
git push -u origin main

To check the remote URL for a repository:

git remote -v

To change the HTTPS remote URL to the SSH remote URL:

git remote set-url origin git@gitlab.com:username/repository.git

© 2024 baponkar All rights reserved.