When a team collaborates in a Git repository, it’s always wise to fork the main repository and make changes in this separate space. All changes should then be submitted via pull requests, allowing an admin to review and approve. Managing multiple repositories can be tricky, it’s essential to have distinct Git URLs. Cloning a project via HTTPS often prompts for a password each time, which can be cumbersome. Therefore, it’s more convenient to clone projects using SSH. Occasionally, you might need to juggle SSH keys for different accounts. Let’s explore how to generate these SSH keys and associate them with the right accounts.
Generating the SSH keys
- Navigate to the
.ssh
folder from your computer’s home directory. (Paths may vary based on the operating system.) - Launch the terminal and input:
ssh-keygen
When prompted for a file name, for my personal GitHub account, I use therakib7_github. For a Bitbucket account, executing ssh-keygen again, I opt for therakib7_bitbucket.
Within the .ssh folder, you’ll find two .pub files: therakib7_github.pub and therakib7_bitbucket.pub.
Enter file in which to save the key (/home/therakib7/.ssh/id_rsa): therakib7_github
Adding a new SSH key to your GitHub account
To associate the SSH key with GitHub:
- Copy the contents of
therakib7_github.pub
. - Access your GitHub account and go to
Settings
. - Select
SSH and GPG keys
. - Click on
New SSH Key
, provide a meaningfulTitle
, paste the copiedKey
, and finally, save it by clickingAdd SSH Key
.
In the same way, you can add Bitbucket and other git accounts.
Creating and updating the SSH config file
To map your SSH keys to the appropriate services:
If an existing config
file is found within .ssh
, edit it. Otherwise, create one with:
touch config
nano config
Populate or modify the config
file with:
# Github account
Host therakib7_github
HostName github.com
User git
IdentityFile ~/.ssh/therakib7_github
# Bitbucket account
Host therakib7_bitbucket
HostName bitbucket.org
User git
IdentityFile ~/.ssh/therakib7_bitbucket
The above configuration supports both GitHub and Bitbucket accounts, offering a smooth experience when managing SSH for these repositories.
Cloning the repositories
For GitHub:
git clone git@therakib7_github:therakib7/private-project-repo.git
//git@{host}:{username}/{repository_name}.git
For Bitbucket:
git clone git@therakib7_bitbucket:therakib7/private-project-repo.git
Conclusion
Though this guide details the process for a single GitHub and Bitbucket account, the approach is scalable. You can seamlessly manage multiple accounts as needed.
Leave a Reply