Skip to content

Guide

How to set a different Git email per folder

Git can switch your commit email by folder on its own, with a rule in ~/.gitconfig. Here's the rule, a version that matches by remote instead of folder, and how to check which email a repo will really use.

By the iKnowMyMac team · Updated · 6 min read

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

~/.gitconfig
[user]
    name = Sam Lee
    email = samdev@example.com

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work
~/.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 .git is 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 .git folder, so it takes effect once you git clone or git 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:

~/.gitconfig, Git 2.36 or later
[includeIf "hasconfig:remote.*.url:git@github.com:acme-corp/**"]
    path = ~/.gitconfig-work
[includeIf "hasconfig:remote.*.url:https://github.com/acme-corp/**"]
    path = ~/.gitconfig-work

Check 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:

~/.gitconfig-work, complete
[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=yes

Check it worked

Inside a work repo
cd ~/work/billing-api
git config --show-origin --get user.email
Expected output
file:/Users/sam/.gitconfig-work	sam@acme.dev

To 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

  • --show-origin points 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 includeIf block sits above [user]
    Fix
    Move the block to the end of the file
  • A hasconfig rule never matches

    Cause
    Git older than 2.36, or the pattern doesn't match the URL exactly
    Fix
    git --version, and compare with git remote -v
Common reasons and fixes

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.

FAQ

Questions

Something else? support@iknowmymac.com

Does includeIf work with the Git that comes with macOS?

Yes. gitdir: rules have worked since Git 2.13 and hasconfig:remote.*.url: since 2.36. Apple's Git on macOS 15 and later supports both; git --version shows yours.

Which value wins when user.email is set in several places?

The last one Git reads. It reads the system config, then ~/.gitconfig, then the repo's .git/config, and an included file counts as if it were written where the includeIf line is. So a repo's own setting beats every rule.

Can I switch the SSH key per folder too?

Yes. Put core.sshCommand with ssh -i and -o IdentitiesOnly=yes in the included file, and every repo the rule matches pushes with that key.

Does a new rule change my existing commits?

No. Git writes the email into each commit when it's made. The rule affects commits from now on; older ones keep their email unless you rewrite them.

Know why your Mac is slow.

macOS 15 Sequoia or later · Apple silicon. A 11.5 MB native app, notarized by Apple. No account, no analytics, no telemetry.

One-time purchase · every feature · 14-day refund