Imagine you're working with multiple organizations, each managing code versions differently. For example, you may need to create a new Git account using your work email while you already have Git set up for your personal account. In another scenario, each organization might use a different version control platform, such as GitHub, GitLab, or Bitbucket.
This quick guide will walk you through the steps of managing such a set up.
Setting Up Multiple Git Accounts Using SSH
We will cover two types of setups you might encounter:
- Multiple authentication methods on a single version control platform (e.g., GitHub).
- Different version control platforms, each with its own authentication method.
The following is a graphical representation of the SSH authentication setup we will be implementing.
Step 1: Generate Unique SSH Keys
Naming your keys properly is crucial. I recommend using this naming convention: nameofvcplatform_nameoforganization_rsa
. For example, github_tesla_rsa
.
# GitHub Personal Acct
ssh-keygen -t rsa -b 4096 -C "your_email@personal.com" -f ~/.ssh/github_personal_rsa
# GitHub Organization Acct
ssh-keygen -t rsa -b 4096 -C "your_email@organization-name.com" -f ~/.ssh/github_organizationname_rsa
# GitLab
ssh-keygen -t rsa -b 4096 -C "your_email@organization-name.com" -f ~/.ssh/gitlab_organizationname_rsa
# Bitbucket
ssh-keygen -t rsa -b 4096 -C "your_email@organization-name.com" -f ~/.ssh/bitbucket_organizationname_rsa
Step 2: Add SSH Keys to SSH Agent
In our case this would be:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/github_personal_rsa
ssh-add ~/.ssh/github_organizationname_rsa
ssh-add ~/.ssh/gitlab_organizationname_rsa
ssh-add ~/.ssh/bitbucket_organizationname_rsa
Step 3: Add SSH Keys to Each Platform
Copy the public key and add it to your Git account. On Linux, use the cat
command to display the key. Alternatively, find the appropriate command for your OS or simply open the key file and copy its contents."
# Copy public key
cat ~/.ssh/github_personal_rsa
cat ~/.ssh/github_organizationname_rsa
cat ~/.ssh/gitlab_organizationname_rsa
cat ~/.ssh/bitbucket_organizationname_rsa
GitHub: Go to [SSH and GPG Keys(https://github.com/settings/keys).
GitLab: Go to SSH Keys.
Bitbucket: Go to SSH Keys.
Step 4. Configure SSH Config File
Create or edit the ~/.ssh/config file to define how SSH should handle each account.
The SSH config file is essential because it defines the hosts, enabling you to distinguish between different version control platforms and accounts by their names. Take a look at the SSH config for our repositories.
Note how Host
and IdentityFile
are configured.
# GitHub Personal
Host github-personal.com
HostName github.com
User git
IdentityFile ~/.ssh/github_personal_rsa
# GitHub Organization
Host github-organizationname.com
HostName github.com
User git
IdentityFile ~/.ssh/github_organizationname_rsa
# GitLab
Host gitlab-organizationname.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/gitlab_organizationname_rsa
# Bitbucket
Host bitbucket-organizationname.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/bitbucket_organizationname_rsa
Step 5: Using the repositories
The commands for cloning, pulling, or pushing will change based on the custom host names defined in the SSH config
For example:
1. Cloning Repositories
Instead of using the default host names (e.g., github.com, gitlab.com), you'll use the custom host names defined in the SSH config:
GitHub Personal:
git clone git@github-personal.com:username/repo.git
GitHub Organization:
git clone git@github-organizationname.com:organization/repo.git
**GitLab:**
git clone git@gitlab-organizationname.com:organization/repo.git
Bitbucket:
git clone git@bitbucket-organizationname.org:organization/repo.git
2. Pulling or Pushing Changes
For pulling or pushing changes, you will use the same format, referencing the custom host names:
GitHub Personal:
git pull git@github-personal.com:username/repo.git
git push git@github-personal.com:username/repo.git
GitHub Organization:
git pull git@github-organizationname.com:organization/repo.git
git push git@github-organizationname.com:organization/repo.git
GitLab:
git pull git@gitlab-organizationname.com:organization/repo.git
git push git@gitlab-organizationname.com:organization/repo.git
Bitbucket:
git pull git@bitbucket-organizationname.org:organization/repo.git
git push git@bitbucket-organizationname.org:organization/repo.git
3. Setting Remote URL for Existing Repositories
If you want to update the remote URL for an existing repository, you can use the following command:
GitHub Personal:
git remote set-url origin git@github-personal.com:username/repo.git
GitHub Organization:
git remote set-url origin git@github-organizationname.com:organization/repo.git
GitLab:
git remote set-url origin git@gitlab-organizationname.com:organization/repo.git
Bitbucket:
git remote set-url origin git@bitbucket-organizationname.org:organization/repo.git
These adjustments ensure that Git uses the appropriate SSH key for the specified host, as defined in your SSH config file.
I hope you found the article helpful. Thank you for reading!
Top comments (0)