English
Complete Guide to Configuring Multiple SSH Accounts in Git (Cross-Platform Universal)

Problem Scenario
No matter what you use macOS, Linux, or Windows, as long as they are used simultaneously on the same computer Company Git account(such as internal GitLab) and Personal Git account(such as GitHub), you will encounter:
SSH key conflict: Default
~/.ssh/id_rsaCan only be paired with one account.Commit history is messy: Global
user.name/user.emailwill make all commits become the same person
This article provides Cross-platform universal solution, platform-specific differences will be specifically noted.
1. Remove Global Account Settings
bash
git config --global --unset user.name
git config --global --unset user.email💡 Verification:git config --global --list
II. Generate an independent SSH key pair
bash
# Default account (e.g., fly)
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa
# Personal account (e.g., fishfinal)
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_fishfinal📁 Default key storage path:~/.ssh/(Windows is %USERPROFILE%\.ssh\)
3. Configure the SSH Config file (Core)
Create/Edit ~/.ssh/config(Windows path:%USERPROFILE%\.ssh\config):
bash
# Default account (fly) - use github.com
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# Personal account (fishfinal) - Use a subdomain alias
Host fishfinal.github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_fishfinal🔑Core Design Philosophy: Use the GitHub account name as the second-level domain prefix, such as fishfinal.github.com, with clear semantics and clear at a glance.
IV. Add the key to SSH Agent
macOS
bash
# Add and save to Keychain (avoid re-entering your password)
ssh-add --apple-use-keychain ~/.ssh/id_rsa
ssh-add --apple-use-keychain ~/.ssh/id_rsa_fishfinal💡 Note
In newer versions of macOS/OpenSSH,--apple-use-keychain The option may have been deprecated. If an error occurs during execution, please use the following instead:
bash
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_fishfinalLinux (Ubuntu/Debian/CentOS, etc.)
bash
# Start ssh-agent (if not running)
eval "$(ssh-agent -s)"
# Add key
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_fishfinal
# Optional: Permanently remember password (requires installing keychain)
# sudo apt install keychain # Debian/Ubuntu
# sudo yum install keychain # CentOS
# echo 'eval $(keychain --eval --agents ssh id_rsa id_rsa_fishfinal)' >> ~/.bashrcWindows (Git Bash / PowerShell / WSL2)
Git Bash method:
bash
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_fishfinalPowerShell (with OpenSSH):
powershell
# Start the ssh-agent service
Start-Service ssh-agent
# Add key
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_fishfinalWSL2 (Ubuntu): Same as the Linux method
V. Add the public key to the Git service provider
Copy public key (common method for all platforms):
bash
# macOS / Linux
cat ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa_fishfinal.pub
# Or copy directly to the clipboard
pbcopy < ~/.ssh/id_rsa_fishfinal.pub # macOS
xclip -sel clip < ~/.ssh/id_rsa_fishfinal.pub # Linux (requires installing xclip)
# Windows (Git Bash)
cat ~/.ssh/id_rsa_fishfinal.pub
# PowerShell
Get-Content ~/.ssh/id_rsa_fishfinal.pub | Set-ClipboardThen add each public key separately to the corresponding GitHub account's Settings → SSH and GPG keys.
VI. Test Connection
bash
# Test default account (fly)
ssh -T [email protected]
# Successfully output: Hi fly! You've successfully authenticated...
# Test personal account (fishfinal)
ssh -T [email protected]
# Output successful: Hi fishfinal! You've successfully authenticated...🐧 If Linux users encounter permission issues:chmod 600 ~/.ssh/id_rsa_*
VII. Usage Configuration (Practical Operation)
1. Clone a new repository
bash
# For projects under the default account (fly), use github.com directly.
git clone [email protected]:fly/some-repo.git
# Projects under the personal account (fishfinal), using a second-level domain alias.
git clone [email protected]:fishfinal/go-gateway-demo.git2. Modify the remote address of an existing repository
bash
cd existing-project
# Change to the repository address of the fishfinal account.
git remote set-url origin [email protected]:fishfinal/go-gateway-demo.git
# Verify the modification result
git remote -v3. Set user information separately for each project
bash
# Enter the personal project directory
cd ~/projects/go-gateway-demo
git config user.name "fishfinal"
git config user.email "[email protected]"
# Enter the default project directory
cd ~/work/default-project
git config user.name "fly"
git config user.email "[email protected]"⚠️Don't add --global, this way it only affects the current project.
4. Verify configuration
bash
# View the user configuration for the current project
git config user.name
git config user.email
# View the remote repository address
git remote -v
# Test push permission
git push --dry-run8. Summary of Solution Advantages
| Characteristics | Description |
|---|---|
| Clear semantics | fishfinal.github.com You can tell at a glance which account it corresponds to. |
| No need to memorize | The alias itself is a meaningful domain name, not a random abstract name. |
| Friendly by default | github.com As the default account, daily operations carry zero mental burden. |
| Intuitive switching | Simply add the account name before the domain name to switch. |
9. Cross-Platform Command Quick Reference
| Operation | macOS | Linux | Windows (Git Bash) |
|---|---|---|---|
| Copy public key to clipboard | pbcopy < ~/.ssh/id_rsa.pub | xclip -sel clip < ~/.ssh/id_rsa.pub | `cat ~/.ssh/id_rsa.pub |
| SSH key storage location | ~/.ssh/ | ~/.ssh/ | %USERPROFILE%\.ssh\ |
| Permission repair | Generally not necessary | chmod 600 ~/.ssh/id_rsa_* | Generally not necessary |
| SSH Agent persistence | --apple-use-keychain | keychain tool | Windows service automatic startup |
Troubleshooting common issues
Permission denied (publickey)
Check whether the public key has been added to the corresponding GitHub account.
Linux/Mac Check key permissions:
chmod 600 ~/.ssh/id_rsa_*Execute
ssh -vT git@你的Host别名View detailed logs
It always returns the same account during testing.
Check
~/.ssh/configwhether multiple Hosts use the same IdentityFile inVerify that the correct key is loaded in ssh-agent:
ssh-add -luse
ssh -T -i ~/.ssh/指定私钥 [email protected]Test each key individually
ssh-add fails under Windows
powershell
# Run PowerShell as administrator
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
ssh-add ~/.ssh/id_rsa_fishfinalAfter Linux restarts, the key needs to be added again.
Install keychain Tool permanence:
bash
# Ubuntu/Debian
sudo apt install keychain
# Add to ~/.bashrc
echo 'eval $(keychain --eval --agents ssh id_rsa id_rsa_fishfinal)' >> ~/.bashrc
source ~/.bashrcThe commit author is still the old information.
Confirm that it has been executed
git config --global --unset user.name/user.emailunder the inspection items
.git/configThe file confirms that the local configuration has taken effect.
Summarize the flowchart
Reference materials
If this article has been helpful to you, feel free to share it with developers who also need multi-account management!