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.
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.
Add the private keys to the corresponding platforms.
Get public keys
cat ~/.ssh/id_rsa_one.pub cat ~/.ssh/id_rsa_two.pubCreate 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 twoRemote test (optional)
ssh -T one.aliyun.com ssh -T two.aliyun.comClone 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.gitOptionally 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
