In real-world development, we often need to use multiple Git accounts on the same computer, for example:
- Personal GitHub account
- Company GitHub account
- GitLab account
- Gitee account
If you simply use the default Git configuration, you can easily run into these problems:
- Using the wrong account when running
git push - Commit history showing up under a different account
- Not knowing which SSH Key to use
- Frequently modifying
user.nameanduser.emailacross different projects - SSH configurations for GitHub, GitLab, and other platforms interfering with each other
The most stable solution to these problems is:
One account per SSH Key, distinguish accounts through SSH Config, and determine commit identity through project-level Git Config.
Below, using Windows as an example, we’ll build a complete Git multi-account workflow.
1. Understanding the Two Identities in Git Multi-Account Setup
Before configuring, you need to distinguish between two easily confused concepts.
1. Git Commit Identity
Every Git commit records:
user.name
user.email
For example:
git config user.name "zhangsan"
git config user.email "zhangsan@example.com"
What it determines is:
Who submitted this commit.
2. Git Repository Authentication Identity
When executing:
git pull
git push
Servers like GitHub and GitLab also need to verify:
Do you have permission to access this repository?
If using SSH, this identity is determined by the SSH Key.
Therefore:
Git Config
↓
Determines who the commit is from
SSH Key
↓
Determines which account is used to access the remote repository
These two identities need to be configured separately.
2. Viewing Current Git Configuration
First, view the global configuration:
git config --global --list
Pay attention to:
user.name=xxx
user.email=xxx
You can also view them separately:
git config --global user.name
git config --global user.email
If you previously had only one Git account, you likely already have a global identity configured:
git config --global user.name "your-name"
git config --global user.email "your-email@example.com"
For a multi-account environment, I recommend:
Don’t rely on a single unified global user.name / user.email. Instead, set the corresponding identity in each project.
3. Generating Different SSH Keys for Different Accounts
Assume you now have two GitHub accounts:
Personal account: personal
Work account: work
Open PowerShell or Git Bash.
First, check existing SSH Keys:
ls ~/.ssh
The actual Windows directory is typically:
C:\Users\YourUsername\.ssh
Then create two separate keys.
Personal account:
ssh-keygen -t ed25519 -C "personal@example.com"
Save as:
~/.ssh/id_ed25519_personal
Work account:
ssh-keygen -t ed25519 -C "work@example.com"
Save as:
~/.ssh/id_ed25519_work
The final directory should look like:
.ssh/
├── id_ed25519_personal
├── id_ed25519_personal.pub
├── id_ed25519_work
└── id_ed25519_work.pub
Where:
id_ed25519_xxx
is the private key — never upload or share it with anyone.
And:
id_ed25519_xxx.pub
is the public key, which can be added to code hosting platforms like GitHub and GitLab.
4. Adding SSH Public Keys to Code Hosting Platforms
View the personal account public key:
cat ~/.ssh/id_ed25519_personal.pub
View the work account public key:
cat ~/.ssh/id_ed25519_work.pub
Copy the corresponding content and add each to the respective Git account’s SSH Keys.
For example:
personal GitHub
↑
id_ed25519_personal.pub
work GitHub
↑
id_ed25519_work.pub
This way, each account has its own independent SSH identity.
5. Using SSH Config to Distinguish Different Accounts
This is the most critical step in the entire multi-account configuration.
Create or edit:
C:\Users\YourUsername\.ssh\config
Note that the filename is simply:
config
No .txt extension.
Assuming both accounts use GitHub, you can configure:
# Personal GitHub
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
# Work GitHub
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
The most important parts here are:
Host github-personal
and:
Host github-work
These are SSH aliases we define ourselves.
In reality, both ultimately access:
github.com
But SSH will select different keys based on different Host aliases.
That is:
github-personal
↓
github.com
↓
id_ed25519_personal
And:
github-work
↓
github.com
↓
id_ed25519_work
This solves the problem:
How to use two different accounts on the same github.com.
6. Testing Both SSH Accounts
After configuration, test each separately.
Personal account:
ssh -T git@github-personal
Work account:
ssh -T git@github-work
If configured correctly, GitHub will return something like:
Hi username! You've successfully authenticated...
Check whether the returned username matches the expected account.
If the two commands are recognized as two different GitHub users, the SSH multi-account configuration is successful.
7. Choosing the Correct Account When Cloning Repositories
A standard GitHub SSH URL is typically:
git@github.com:user/repository.git
After configuring multi-account, you no longer use:
github.com
directly, but instead use the Host defined in your SSH Config.
For example, for a personal project:
git clone git@github-personal:personal-user/my-project.git
For a work project:
git clone git@github-work:company/my-project.git
Note that:
github-personal
github-work
are not real domain names.
SSH will automatically map them to:
github.com
based on:
~/.ssh/config
while selecting the correct SSH Key.
8. How to Switch Accounts for Existing Repositories
If a project has already been cloned, you can view the current remote URL:
git remote -v
You might see:
origin git@github.com:company/project.git
If this is a work account project, you can change it to:
git remote set-url origin git@github-work:company/project.git
Check again:
git remote -v
It should now show:
origin git@github-work:company/project.git
From now on, when you run:
git pull
git push
SSH will automatically use:
id_ed25519_work
for authentication.
9. Configuring the Correct Git Identity for Each Project
SSH solved the problem of “which account to use for repository access.”
Next, we need to solve:
Whose name and email should the commit display?
Enter the personal project:
cd my-personal-project
Set:
git config user.name "personal-name"
git config user.email "personal@example.com"
Enter the company project:
cd my-work-project
Set:
git config user.name "work-name"
git config user.email "work@company.com"
Note that you should not add --global here.
For example:
git config user.email "work@company.com"
means:
This only applies to the current repository.
Whereas:
git config --global user.email "work@company.com"
would affect all Git projects on the entire computer.
You can use the following commands to confirm the final identity used in the current project:
git config user.name
git config user.email
If you want to further confirm where the configuration comes from:
git config --show-origin --get user.name
git config --show-origin --get user.email
10. Recommended Complete Multi-Account Structure
Ultimately, a clear Windows Git multi-account structure looks like this:
Windows
│
├── ~/.ssh/
│ │
│ ├── id_ed25519_personal
│ ├── id_ed25519_personal.pub
│ │
│ ├── id_ed25519_work
│ ├── id_ed25519_work.pub
│ │
│ └── config
│
├── Personal Project
│ │
│ ├── Git Config
│ │ ├── user.name = personal-name
│ │ └── user.email = personal@example.com
│ │
│ └── origin
│ └── git@github-personal:user/project.git
│
└── Work Project
│
├── Git Config
│ ├── user.name = work-name
│ └── user.email = work@company.com
│
└── origin
└── git@github-work:company/project.git
This forms two independent control layers:
Project Git Config
↓
Controls commit identity
Remote URL
↓
SSH Config Host
↓
IdentityFile
↓
Controls remote repository authentication identity
Once you understand this, Git multi-account is actually quite simple.
11. Using GitHub + GitLab + Gitee Simultaneously
If instead of two GitHub accounts, you have multiple different platforms, the configuration method is the same.
For example:
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
Host gitlab-work
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab
IdentitiesOnly yes
Host gitee-personal
HostName gitee.com
User git
IdentityFile ~/.ssh/id_ed25519_gitee
IdentitiesOnly yes
Then simply choose different URLs based on the repository:
git@github-personal:user/project.git
git@github-work:company/project.git
git@gitlab-work:company/project.git
git@gitee-personal:user/project.git
So this solution doesn’t depend on any specific platform — it’s essentially using SSH Config to manage multiple SSH identities.
12. Further Optimization: Auto-Switching Git Identity by Directory
If you have many projects, manually running for each one:
git config user.name ...
git config user.email ...
is still tedious.
Git natively supports includeIf, which can automatically load different configurations based on the project’s directory.
For example, let’s define:
D:\project\personal\
D:\project\work\
to store personal and company projects respectively.
Edit:
~/.gitconfig
Configure:
[includeIf "gitdir:D:/project/personal/"]
path = ~/.gitconfig-personal
[includeIf "gitdir:D:/project/work/"]
path = ~/.gitconfig-work
Create:
~/.gitconfig-personal
Content:
[user]
name = personal-name
email = personal@example.com
Then create:
~/.gitconfig-work
Content:
[user]
name = work-name
email = work@company.com
This way, as long as a project is located in:
D:\project\personal\
Git will automatically use the personal identity.
If the project is located in:
D:\project\work\
It will automatically use the work identity.
Ultimately, you can achieve:
Windows
│
┌─────────┴─────────┐
↓ ↓
D:\project\personal D:\project\work
│ │
↓ ↓
personal Git Config work Git Config
│ │
↓ ↓
github-personal github-work
│ │
↓ ↓
Personal SSH Key Work SSH Key
This is the approach I recommend for long-term use.
13. Daily Workflow
After a one-time configuration, daily development is actually very simple.
Personal project:
cd D:\project\personal
git clone git@github-personal:user/project.git
Work project:
cd D:\project\work
git clone git@github-work:company/project.git
Then use normally:
git add .
git commit -m "update"
git pull
git push
That’s it.
No need to switch GitHub login accounts daily, and no need to repeatedly modify SSH Keys.
Summary
Using multiple Git accounts simultaneously on a single Windows computer essentially requires solving two problems:
First, whose identity does the commit use?
Controlled by:
git config user.name
git config user.email
Second, whose permission does Push use?
Controlled by:
SSH Config
↓
Host Alias
↓
IdentityFile
Therefore, a stable multi-account solution can be summarized as:
One Git account
↓
One SSH Key
↓
One SSH Host Alias
↓
One corresponding Remote URL
Combined with:
Git includeIf
↓
Based on project directory
↓
Automatically select user.name / user.email
You can build a Git multi-account development environment on Windows that requires virtually no manual switching.
Configure once, and personal projects, company projects, and different code repositories like GitHub, GitLab, and Gitee can all be used simultaneously without interfering with each other.