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.
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.
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/
.
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_username1
LogIn into the GitHub Account with username1@example.com
with password.
Navigate to Settings > SSH Keys.
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.
To activate SSH for username2
you need to perform all above four steps for username2
.
Now create a config file by ~/.ssh/config
inside of ~/.ssh/
directory location.
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 clone https://github.com/username/repository.git
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@github.com:username/repository.git
git push -u origin main
git remote -v
git remote set-url origin git@github.com:username/repository.git
Follow similar steps as above to set up SSH keys for GitLab accounts. The documentation for GitLab SSH keys can be found here.
ssh-keygen -t rsa -b 4096 -C "username1@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
Log in to your GitLab account with username1@example.com and your password.
Navigate to User Settings > SSH Keys.
ssh -T git@gitlab.com
If everything is correct, you will see a message like:
Welcome to GitLab, @username1!
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
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.