Fundamentals 5 min read

How to Manage Multiple Git Accounts on One Machine with Separate SSH Keys

This guide explains how to configure a single computer to use multiple Git accounts by generating distinct SSH keys, setting up a custom SSH config file, adding the keys to each platform, and cloning repositories with account-specific credentials.

Programmer DD
Programmer DD
Programmer DD
How to Manage Multiple Git Accounts on One Machine with Separate SSH Keys

When you need to use multiple Git accounts on a single computer, you must configure separate SSH keys for each platform.

Idea

Manage multiple SSH keys simultaneously.

Solution

Generate multiple SSH keys

Example using two accounts (one and two). Ensure you run the commands inside the ~/.ssh directory and consider cleaning the directory first.

ssh-keygen -t rsa -C "[email protected]"
ssh-keygen -t rsa -C "[email protected]"

When prompted to "Enter file in which to save the key", rename the files (e.g., id_rsa_one and id_rsa_two) to generate four files: private and public keys for each account.

SSH key files
SSH key files

Add the private keys to the corresponding platforms.

Get public keys

cat ~/.ssh/id_rsa_one.pub
cat ~/.ssh/id_rsa_two.pub

Create a config file in ~/.ssh: touch config Then add the following configuration:

# git server one
Host one.aliyun.com
Hostname code.aliyun.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_one
User one

# git server two
Host two.aliyun.com
Hostname code.aliyun.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_two
User two

Remote test (optional)

ssh -T one.aliyun.com
ssh -T two.aliyun.com

Clone repositories

Old syntax: [email protected]:project_path.git New syntax using the defined hosts:

git clone [email protected]:project_path.git
git clone [email protected]:project_path.git

Optionally set repository‑specific user name and email:

git config user.name "one_name"; git config user.email "one_email"
git config user.name "two_name"; git config user.email "two_email"

Conclusion

This article summarizes a practical approach to handling multiple Git accounts on one machine by generating separate SSH keys, configuring the SSH client, and using account‑specific clone URLs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitSSHssh-configgit clonemultiple accounts
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.