How to Use Multiple Git Accounts on One Machine (Work & Personal)
Introduction
Brief context on why this is really useful — multiple companies, personal projects, the frustration of wrong commits or push errors.
💡 Why This Is Useful
If you contribute to code across multiple Git accounts.
for example, one for your day job, another for personal projects, and maybe even a freelance client
it’s easy to run into issues:
- Accidentally committing code with the wrong name or email
- Pushing to GitHub or GitLab with the wrong SSH key, leading to authentication errors
- Having to manually switch configs every time you jump between projects
This guide helps you avoid all of that. With the right setup, Git will automatically use the correct identity, email, and SSH key — all based on the folder your repo is in. No more manual switching. No more commit regrets.
🧠 What You’ll Learn
- How to set up multiple SSH keys for different Git accounts (work, personal, freelance, etc.)
- How to configure Git to automatically switch user profiles based on project location
- How to use
includeIf
conditionals in.gitconfig
to avoid global misconfigurations - How to remove global Git identity safely while still preserving other config options
- How to set a custom SSH command per repo for Git to use the right credentials
- How to structure your dev folders to keep things clean and context-aware
- How to avoid common mistakes like pushing to GitHub with the wrong email
🧭 Step-by-Step Overview
Here’s a breakdown of what we’ll cover in this guide:
🔑 Step 1: Generate SSH Keys for Each Account
Create separate SSH keys for your work and personal Git profiles.
🧹 Step 2: Remove Global Git User Identity
Clear your global Git user config to prevent accidental misattribution.
📂 Step 3: Create a Config Directory for Git
Set up a dedicated directory to manage Git config files for each account.
📄 Step 4: Create Separate Config Files for Each Account
Add work.config
and personal.config
to store identity and SSH info.
✍️ Step 5: Set Account Info in Each Config File
Define your name, email, and SSH command in each respective config file.
🗂️ Step 6: Organize Your Repositories by Context
Structure your projects into ~/dev/work/
and ~/dev/personal/
.
⚙️ Step 7: Use Conditional Includes in Your Git Config
Configure Git to load the right settings based on the repo’s location.
🧪 Step 8: Test the Setup
Verify that Git uses the correct identity and SSH key for each repo.
✅ Conclusion
Optional: Skip the SSH setup if you use HTTPS and access tokens instead.
🛠️ The Step-by-Step Guide
Follow these steps to set up multiple Git profiles with their own SSH keys and identities.
🔑 Step 1: Generate SSH Keys for Each Account
- Create separate SSH keys for your work and personal accounts :
# For work
ssh-keygen -t ed25519 -C "workemail@company.com" -f ~/.ssh/work_id_ed25519
# For personal
ssh-keygen -t ed25519 -C "personalemail@gmail.com" -f ~/.ssh/personal_id_ed25519
- You should now see these files in your
~/.ssh
directory:
work_id_ed25519
work_id_ed25519.pub
personal_id_ed25519
personal_id_ed25519.pub
- Add the keys to the SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/personal_id_ed25519
ssh-add ~/.ssh/work_id_ed25519
- If you previously have a global Git user in
~/.gitconfig
, remove the content by commenting it:
# ~/.gitconfig
# Remove or comment out this block:
# [user]
# name = Your Name
# email = your@email.com
📂 Step 3: Create a Config Directory for Git
- Create a directory to hold account-specific Git configurations:
mkdir -p ~/.config/git
📄 Step 4: Create Separate Config Files for Each Account
- Make two separate config files, you can even create more if you want:
touch ~/.config/git/work.config
touch ~/.config/git/personal.config
- Check the directory:
ls ~/.config/git
# Output:
# personal.config
# work.config
✍️ Step 5: Set Account Info in Each Config File
- for personal:
~/.config/git/personal.config
:
[user]
name = Your Name
email = personalemail@gmail.com
[core]
sshCommand = "ssh -i ~/.ssh/personal_id_ed25519"
- for work:
~/.config/git/work.config
:
[user]
name = Your Name
email = workemail@company.com
[core]
sshCommand = "ssh -i ~/.ssh/work_id_ed25519"
🗂️ Step 6: Organize Your Repositories by Context
- To make this setup work smoothly, structure your projects like this:
~/dev/work/ # All work-related repos
~/dev/personal/ # All personal projects repos
Clone or move your repos into the appropriate folders.
⚙️ Step 7: Use Conditional Includes in Your Git Config
Open ~/.gitconfig
and add conditional logic to include the right config based on directory:
[includeIf "gitdir:~/dev/work/"] # the path to the work directory
path = ~/.config/git/work.config # the path to the work config
[includeIf "gitdir:~/dev/personal/"] # the path to the personal directory
path = ~/.config/git/personal.config # the path to the personal config
💡 Pro Tip: Make sure the path =
lines don’t have quotes — just a plain file path.
🧪 Step 8: Test the Setup
- Open a new terminal, navigate into one of your repos, and check the Git config:
cd ~/dev/personal/my-side-project
git config user.name
git config user.email
git config core.sshCommand
git config --list
You should see your personal identity.
- Now try a work repo:
cd ~/dev/work/cool-client-project
git config user.name
git config user.email
git config core.sshCommand
git config --list
You should see your work identity.
✅ Conclusion
With this setup, you no longer have to worry about using the wrong Git identity across different projects. Everything works automatically based on where your repo lives — clean, efficient, and frustration-free.
However, if you use HTTPS with personal access tokens (like GitHub or GitLab tokens) instead of SSH, you can skip the SSH setup entirely.
In that case, all you need in each config file is:
[user]
name = Your Name
email = personal@gmail.com
If this guide saved you from yelling at Git in frustration, go ahead and give it a clap — it helps more devs find it.
So moving forward, more helpful, no-nonsense dev content is on the way, so feel free to follow me if you’re into clean setups, productivity hacks, and an occasional rant about Tech generally.
Thanks for reading — now go commit responsibly 👏!