The short answer
Add an includeIf block to ~/.gitconfig that loads a second file for one folder: [includeIf "gitdir:~/work/"] with path = ~/.gitconfig-work under it, and put [user] with email = sam@acme.dev in that file. Every repo under ~/work/ then commits with the work email and everything else uses your global one. To match by remote instead, use [includeIf "hasconfig:remote.*.url:git@github.com:acme-corp/**"], which needs Git 2.36 or later. Check with git config --show-origin --get user.email inside a repo.
On this page
Set it up by folder
[user]
name = Sam Lee
email = samdev@example.com
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work[user]
email = sam@acme.dev- Keep the trailing slash.
gitdir:~/work/matches every repo anywhere under~/work, because Git adds**after a trailing slash. Without it, the rule only matches a repo whose.gitis exactly that path. - Put the rule after `[user]`. Git reads the file top to bottom and the last value wins, so a rule above your global email is overridden by it.
- `gitdir/i:` matches without regard to case. The Mac's disk ignores case by default, so it helps if you sometimes type
~/Work. - It only applies inside a repo. The rule matches the repo's
.gitfolder, so it takes effect once yougit cloneorgit init, not in an empty folder.
Set it up by remote owner
If work repos live all over your disk, match on the remote instead. hasconfig:remote.*.url: applies the file to any repo with a remote URL that matches the pattern. Add the HTTPS form too if you clone both ways:
[includeIf "hasconfig:remote.*.url:git@github.com:acme-corp/**"]
path = ~/.gitconfig-work
[includeIf "hasconfig:remote.*.url:https://github.com/acme-corp/**"]
path = ~/.gitconfig-workCheck your version with git --version. Apple's Git on macOS 15 and later is newer than 2.36. A file included this way isn't allowed to set remote URLs itself.
Add the SSH key and signing key too
The same included file can carry everything else that should change with the email. core.sshCommand pins the push key without touching ~/.ssh/config:
[user]
email = sam@acme.dev
signingkey = ~/.ssh/id_work.pub
[gpg]
format = ssh
[commit]
gpgsign = true
[core]
sshCommand = ssh -i ~/.ssh/id_work -o IdentitiesOnly=yesCheck it worked
cd ~/work/billing-api
git config --show-origin --get user.emailfile:/Users/sam/.gitconfig-work sam@acme.devTo see every value Git found, in the order it read them, use git config --show-origin --show-scope --get-all user.email. The last line is the one that applies.
When the rule doesn't apply
| Symptom | Cause | Fix |
|---|---|---|
--show-origin points at .git/config | The repo sets its own email, which wins over every rule | git config --local --unset user.email |
It points at ~/.gitconfig | The rule didn't match: no trailing slash, or the repo is outside the folder | Compare the rule with git rev-parse --absolute-git-dir |
| The global email wins | The includeIf block sits above [user] | Move the block to the end of the file |
A hasconfig rule never matches | Git older than 2.36, or the pattern doesn't match the URL exactly | git --version, and compare with git remote -v |
--show-originpoints at.git/config- Cause
- The repo sets its own email, which wins over every rule
- Fix
git config --local --unset user.email
It points at
~/.gitconfig- Cause
- The rule didn't match: no trailing slash, or the repo is outside the folder
- Fix
- Compare the rule with
git rev-parse --absolute-git-dir
The global email wins
- Cause
- The
includeIfblock sits above[user] - Fix
- Move the block to the end of the file
A
hasconfigrule never matches- Cause
- Git older than 2.36, or the pattern doesn't match the URL exactly
- Fix
git --version, and compare withgit remote -v
New rules only affect new commits. Commits already made keep the email they were made with; amending or rebasing them is the only way to change it.
The faster way: Identity profiles in iKnowMyMac
iKnowMyMac writes these same includeIf rules for you. A profile holds the commit name and email, the signing key, the SSH key and the GitHub CLI account, and rules apply it by folder or by remote owner.