Set Up Multiple Git Profile on the Same Machine
A one-time SSH and Git configuration that lets your work and personal GitHub accounts coexist on the same machine without ever switching between them.
On this page
If you use the same machine for work and personal development, you've probably run into at least one of these:
- Git commits have the wrong author email.
git pushauthenticates using the wrong GitHub account.- You keep logging in and out of VS Code.
This guide walks through a one-time setup that lets both accounts coexist peacefully - the right commit author, the right SSH key, the right remote - all based on where the repository lives on your machine.
Folder structure
The entire setup hinges on keeping work and personal repositories in separate folders:
~/Documents/Code/GitHub/
├── Work/
└── Personal/
This isn't just for organization. Git has a feature called includeIf that applies different configuration files depending on the directory a repository is in. By keeping repositories separated this way, Git can automatically use the correct name, email, and SSH key without you doing anything per-repository.
Step 1: Create two SSH keys
Generate one key per account, giving each a descriptive filename:
ssh-keygen -t ed25519 -C "your-work@email.com" -f ~/.ssh/github_work
ssh-keygen -t ed25519 -C "your-personal@email.com" -f ~/.ssh/github_personal
This creates two key pairs. To add them to GitHub, you first need to copy the public key contents. Run the following for each key:
cat ~/.ssh/github_work.pub
cat ~/.ssh/github_personal.pub
Or copy directly to your clipboard without printing it:
# macOS
pbcopy < ~/.ssh/github_work.pub
# Linux (requires xclip)
xclip -selection clipboard < ~/.ssh/github_work.pub
# Windows (Git Bash)
cat ~/.ssh/github_work.pub | clip
Paste the copied key into the corresponding GitHub account under Settings → SSH and GPG Keys, choosing Authentication as the key type. Repeat for the personal key on your personal account.
You might notice the generated key starts with ssh-ed25519 rather than the
older ssh-rsa. That's expected - ed25519 is the recommended modern
algorithm, and GitHub supports it fully.
Step 2: Configure SSH
Create (or append to) ~/.ssh/config with two host entries - one per account:
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/github_work
IdentitiesOnly yes
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/github_personal
IdentitiesOnly yes
Each Host entry defines a custom alias (github-work, github-personal) that maps to github.com but uses a different SSH key. The IdentitiesOnly yes line is important - it tells SSH to only try the specified key and not fall back to any other identity in your agent.
Verify both connections work:
ssh -T git@github-work
ssh -T git@github-personal
Each should print a success message with the corresponding GitHub username.
Step 3: Configure Git identities
Create two separate Git config files - one per identity:
[user]
name = Your Name
email = your-work@email.com
[user]
name = Your Name
email = your-personal@email.com
Then reference them from your main ~/.gitconfig using includeIf:
[includeIf "gitdir:~/Documents/Code/GitHub/Work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/Documents/Code/GitHub/Personal/"]
path = ~/.gitconfig-personal
From this point on, any repository inside the Work folder automatically uses the work name and email, and any repository inside Personal uses the personal ones. You never need to run git config user.email inside individual repositories again.
Step 4: Update existing remotes to SSH
For any repositories you already have cloned, update their remotes to use the correct SSH host alias:
# For a work repository (replace "acme" with your company's GitHub org or username)
git remote set-url origin git@github-work:acme/my-project.git
# For a personal repository (replace "john" with your personal GitHub username)
git remote set-url origin git@github-personal:john/my-project.git
For new repositories, clone them using SSH from the start. The SSH URL format is always:
git@<ssh-host-alias>:<github-username-or-org>/<repository>.git
Note the colon after the host alias and the slash between the owner and repository name - those aren't interchangeable.
Step 5: Make cloning effortless with shell functions
At this point everything works correctly, but cloning still requires remembering the full SSH URL and the right folder to cd into. A shell function can handle all of that automatically.
The basic version
Here's a straightforward function for cloning work repositories. Add this to your ~/.zshrc or ~/.bashrc:
clone-work() {
# Always clone into the Work folder regardless of where you run this from
cd ~/Documents/Code/GitHub/Work
# Clone using the github-work SSH host alias configured in Step 2
# Replace "acme" with your actual work GitHub username or org name
git clone git@github-work:acme/$1.git
# Jump into the cloned folder
cd $1
}
clone-personal() {
cd ~/Documents/Code/GitHub/Personal
# Replace "john" with your actual personal GitHub username
git clone git@github-personal:john/$1.git
cd $1
}
Now instead of typing the full SSH URL and navigating folders manually, you just run:
clone-work my-project
clone-personal my-blog
The upgraded version
The basic version works, but it will error out if you run it on a repository you've already cloned. The upgraded version handles that gracefully - and also opens VS Code automatically once you're in the folder.
clone-work() {
local ROOT="$HOME/Documents/Code/GitHub/Work"
# Show usage hint if no repo name is passed
[ -z "$1" ] && { echo "Usage: clone-work <repo-name>"; return 1; }
cd "$ROOT" || return
# If the folder already exists locally, skip cloning
if [ -d "$1" ]; then
echo "📂 Opening existing repository..."
else
echo "⬇️ Cloning repository..."
git clone git@github-work:acme/$1.git || return
fi
cd "$1" || return
# Optional: remove this line if you don't use VS Code
code .
}
clone-personal() {
local ROOT="$HOME/Documents/Code/GitHub/Personal"
[ -z "$1" ] && { echo "Usage: clone-personal <repo-name>"; return 1; }
cd "$ROOT" || return
if [ -d "$1" ]; then
echo "📂 Opening existing repository..."
else
echo "⬇️ Cloning repository..."
git clone git@github-personal:john/$1.git || return
fi
cd "$1" || return
# Optional: remove this line if you don't use VS Code
code .
}
Shortening to two-letter aliases (optional)
Once you're comfortable with the functions, you can rename them to shorter aliases. There's nothing special about the names - just pick whatever feels natural:
# Rename the functions above from clone-work/clone-personal to gw/gp
gw() { clone-work "$@"; }
gp() { clone-personal "$@"; }
Or just rename the functions themselves directly to gw and gp in the definition. The final daily usage becomes:
gw api-service # clone (or open) a work repository
gp portfolio # clone (or open) a personal repository
Both commands work from anywhere in your terminal - they always navigate to the correct root folder before doing anything.
After editing your ~/.zshrc or ~/.bashrc, reload it to apply the changes:
source ~/.zshrc # or source ~/.bashrc
Step 6: Add a global pre-push hook
The setup is solid now, but one last thing worth adding is a safety net: a global Git hook that checks you're not pushing to the wrong account before every push.
First, create a global hooks directory and point Git to it:
mkdir -p ~/.githooks
git config --global core.hooksPath ~/.githooks
Then create the hook file and make it executable:
touch ~/.githooks/pre-push
chmod +x ~/.githooks/pre-push
Add the following to ~/.githooks/pre-push:
#!/bin/bash
REMOTE_URL=$(git remote get-url origin 2>/dev/null)
REPO_PATH=$(git rev-parse --show-toplevel 2>/dev/null)
WORK_DIR="$HOME/Documents/Code/GitHub/Work"
PERSONAL_DIR="$HOME/Documents/Code/GitHub/Personal"
# If we're in a Work folder, make sure the remote uses the work SSH host
if [[ "$REPO_PATH" == "$WORK_DIR"* ]]; then
if [[ "$REMOTE_URL" != *"github-work"* ]]; then
echo "❌ Error: Pushing from a Work repository but remote is not using github-work."
echo " Remote URL: $REMOTE_URL"
echo " Run: git remote set-url origin git@github-work:<org>/<repo>.git"
exit 1
fi
fi
# If we're in a Personal folder, make sure the remote uses the personal SSH host
if [[ "$REPO_PATH" == "$PERSONAL_DIR"* ]]; then
if [[ "$REMOTE_URL" != *"github-personal"* ]]; then
echo "❌ Error: Pushing from a Personal repository but remote is not using github-personal."
echo " Remote URL: $REMOTE_URL"
echo " Run: git remote set-url origin git@github-personal:<username>/<repo>.git"
exit 1
fi
fi
exit 0
This hook runs before every git push. It checks whether the remote URL of the repository matches the folder it lives in - if a repository inside Work is trying to push to a github-personal remote (or vice versa), the push is aborted with a clear error message telling you exactly how to fix it.
This catches the one scenario the folder-based setup can't prevent on its own: a remote URL that was manually set to the wrong account.
Troubleshooting a failed clone
If a clone fails, work through these in order. Each one rules out a different layer.
1. Check which account SSH is using. The output should greet the GitHub username you expect:
ssh -T git@github-work
2. Bypass your shell function. Run the clone by hand with the owner and repository name copied from the Code → SSH option on GitHub. If this works but the alias doesn't, the problem is the namespace or the function, not GitHub:
git clone git@github-work:<owner>/<repo>.git
3. Read the verbose output. This shows which key SSH actually offers, and the last lines contain the real error:
GIT_SSH_COMMAND='ssh -v' git clone git@github-work:<owner>/<repo>.git
Look for a line like Offering public key: ~/.ssh/github_work to confirm the right key is being used.
4. Match the error to the cause:
| Error | What it usually means |
|---|---|
Could not resolve hostname github-work | The Host entry in ~/.ssh/config is missing or misspelled. |
Permission denied (publickey) | The key isn't added to any GitHub account, the IdentityFile path is wrong, or the private key's permissions are too open (fix with chmod 600 ~/.ssh/github_work). |
Repository not found | The owner part of the URL is wrong, or the account has no access to that repository. GitHub says "not found" for private repositories you can't see. For organization-owned repositories, see the optional section. |
The final workflow
Once this is all in place, the daily workflow looks like this:
gw api-service # clone (or open) a work repository
gp portfolio # clone (or open) a personal repository
No account switching. No correcting commit emails after the fact. No logging in and out of VS Code. The right identity and the right SSH key are used automatically, everywhere, based entirely on where the repository lives.
That's the whole setup for most people. If your work repositories are owned by a GitHub organization, or you need a third account for a client, the optional section below covers both.
Optional: organization and client accounts
Everything above is all most setups need. Read this section only if one of these applies to you:
- Your work repositories are owned by a GitHub organization rather than your own account.
- You need a third account, such as a client's, on the same machine.
Authorizing your SSH key for SSO
If your company's GitHub organization enforces SAML single sign-on, adding the key to your account isn't enough. On the SSH and GPG Keys page, find the key, click Configure SSO, and Authorize it for the organization. Until you do, SSH access to that organization's private repositories is refused, even though the same key works fine for your own repositories and HTTPS access may still work. If the button next to the organization says Deauthorize, the key is already authorized.
Cloning repos owned by an organization
There's one gotcha the functions above don't account for: a single hardcoded namespace only works for one place your repos can live. If your work GitHub account is also a member of an organization, org-owned repos live under a completely different path than your own account's repos:
git@github-work:your-username/repo.git # repo under your own account
git@github-work:your-org-name/repo.git # repo owned by the organization
If gw has your username hardcoded and you try to clone a repo that actually belongs to the org, you'll hit a confusing dead end: ssh -T git@github-work still authenticates correctly, the org already shows as SSO-authorized under your SSH key in GitHub's settings, and cloning the exact same repo over HTTPS works fine - yet gw still fails. It's not a permissions problem at all; the function is just pointed at the wrong namespace.
The fix is to accept the namespace as an optional second argument, defaulting to whichever one you use most often:
gw() {
local ROOT="$HOME/Documents/Code/GitHub/Work"
local NAMESPACE="${2:-your-username}" # defaults to your own account, override for orgs
if [ -z "$1" ]; then
echo "Usage: gw <repo-name> [namespace]"
return 1
fi
mkdir -p "$ROOT"
cd "$ROOT" || return
if [ -d "$1" ]; then
echo "📂 Opening existing repository..."
cd "$1"
else
echo "⬇️ Cloning repository..."
git clone git@github-work:"$NAMESPACE"/"$1".git || return
cd "$1"
fi
code .
}
Now both cases work with the same function:
gw my-own-repo # clones your-username/my-own-repo
gw internal-tool your-org-name # clones your-org-name/internal-tool
If you clone org repos more often than your own, just flip the default in NAMESPACE - whichever namespace you'll type less often should be the fallback.
One shared helper, one line per profile
Once you have more than two profiles, copy-pasting the same function for each one gets old, and a fix made in one copy never reaches the others. Move the logic into a single helper, then make every alias a one-liner that passes in its own folder, SSH host, and default namespace:
# ── Shared clone helper ─────────────────────────────────────────────
# Args: command-name, folder, ssh-host, default-namespace, then "$@"
_gitclone() {
local CMD="$1" FOLDER="$2" HOST="$3" DEFAULT_NS="$4" REPO="$5"
local NS="${6:-$DEFAULT_NS}"
local ROOT="$HOME/Documents/Code/GitHub/$FOLDER"
if [ -z "$REPO" ]; then
echo "Usage: $CMD <repo-name> [namespace]"
return 1
fi
mkdir -p "$ROOT"
cd "$ROOT" || return
if [ -d "$REPO" ]; then
echo "📂 Opening existing repository..."
else
echo "⬇️ Cloning repository..."
git clone "git@$HOST:$NS/$REPO.git" || return
fi
cd "$REPO" || return
code .
}
# ── One line per profile ────────────────────────────────────────────
gw() { _gitclone "gw" "Work" "github-work" "your-work-username" "$@"; }
gp() { _gitclone "gp" "Personal" "github-personal" "your-personal-username" "$@"; }
The first four arguments are fixed per profile: the command name (only used in the usage message), the folder under ~/Documents/Code/GitHub, the SSH host alias from Step 2, and the default namespace. The "$@" at the end passes through whatever you typed, so the repository name lands in $5 and the optional namespace in $6. This replaces clone-work, clone-personal, and the standalone gw above, so delete those definitions when you paste it in.
Daily usage stays the same, and the namespace override now works for every profile:
gw my-own-repo # your-work-username/my-own-repo
gw internal-tool your-org-name # your-org-name/internal-tool
gp my-blog # your-personal-username/my-blog
Adding a client or third profile
If you freelance or contract, you may end up with a third GitHub account - a client's - on the same machine. The pattern is exactly the same as before, and each new profile takes five small additions.
If the client simply invited your existing work or personal account as a
collaborator, you don't need a new key at all. Just pass their username or
organization as the namespace, for example gw repo-name client-org. Only
create a separate profile when you have to sign in as a different GitHub
account.
1. Generate a key and add it to that account. Use the same naming pattern, and authorize the key for SSO if that account's organization enforces it:
ssh-keygen -t ed25519 -C "your-client@email.com" -f ~/.ssh/github_client
2. Add the host alias to ~/.ssh/config, then check it with ssh -T git@github-client:
Host github-client
HostName github.com
User git
IdentityFile ~/.ssh/github_client
IdentitiesOnly yes
3. Add the identity. Create the folder, a config file for it, and one more includeIf block in your main ~/.gitconfig (keep the existing ones):
mkdir -p ~/Documents/Code/GitHub/Client
[user]
name = Your Name
email = your-client@email.com
[includeIf "gitdir:~/Documents/Code/GitHub/Client/"]
path = ~/.gitconfig-client
4. Add the alias. With the shared helper from the previous section, this is a single line:
gcli() { _gitclone "gcli" "Client" "github-client" "your-client-username" "$@"; }
Pick alias names that don't collide with ones you already have. Oh My Zsh's git plugin, for example, already defines gc and gcl. Run type <name> before using a name, and reload with source ~/.zshrc afterwards.
5. Teach the hook about it. The Step 6 hook hardcodes two folders. Once you have three or more, replace it with a version that loops over a list of folder:ssh-host pairs, so a new profile is one more line:
#!/bin/bash
REMOTE_URL=$(git remote get-url origin 2>/dev/null)
REPO_PATH=$(git rev-parse --show-toplevel 2>/dev/null)
BASE="$HOME/Documents/Code/GitHub"
# One "folder:ssh-host" pair per profile
PROFILES=(
"Work:github-work"
"Personal:github-personal"
"Client:github-client"
)
for entry in "${PROFILES[@]}"; do
FOLDER="${entry%%:*}"
HOST="${entry##*:}"
if [[ "$REPO_PATH" == "$BASE/$FOLDER/"* && "$REMOTE_URL" != *"$HOST"* ]]; then
echo "❌ Error: This repository lives in $FOLDER/ but its remote is not using $HOST."
echo " Remote URL: $REMOTE_URL"
echo " Run: git remote set-url origin git@$HOST:<owner>/<repo>.git"
exit 1
fi
done
exit 0
It uses a plain array instead of an associative one on purpose, so it also runs on the older Bash that ships with macOS.
With everything from this section in place, the extra daily commands are:
gw internal-tool your-org-name # clone (or open) a repository owned by an organization
gcli client-app # clone (or open) a client repository