dev-resources.site
for different kinds of informations.
How To Configure Multiple Git Accounts with SSH on Your Local Machine
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 setup, applicable to Windows
, Mac OS
, and Linux
. The ~/.ssh/config
files work similarly across Linux
, Mac Os
and Windows
since they are part of the OpenSSH suite
. However, a notable difference is that Linux
& Mac Os
typically come with SSH pre-installed and accessible in the PATH
, whereas on Windows
, OpenSSH
often needs to be installed manually.
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 "[email protected]" -f ~/.ssh/github_personal_rsa
# GitHub Organization Acct
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/github_organizationname_rsa
# GitLab
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/gitlab_organizationname_rsa
# Bitbucket
ssh-keygen -t rsa -b 4096 -C "[email protected]" -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 [email protected]:username/repo.git
GitHub Organization:
git clone [email protected]:organization/repo.git
**GitLab:**
git clone [email protected]:organization/repo.git
Bitbucket:
git clone [email protected]: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 [email protected]:username/repo.git
git push [email protected]:username/repo.git
GitHub Organization:
git pull [email protected]:organization/repo.git
git push [email protected]:organization/repo.git
GitLab:
git pull [email protected]:organization/repo.git
git push [email protected]:organization/repo.git
Bitbucket:
git pull [email protected]:organization/repo.git
git push [email protected]: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 [email protected]:username/repo.git
GitHub Organization:
git remote set-url origin [email protected]:organization/repo.git
GitLab:
git remote set-url origin [email protected]:organization/repo.git
Bitbucket:
git remote set-url origin [email protected]: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!
Featured ones: