Stop Switching GitHub Accounts: Let Your Folders Decide
Configure Git and the GitHub CLI to pick the right account automatically based on which directory you're in. No tokens in URLs, no gh auth switch, no wrong-author commits.
One laptop, two GitHub accounts: personal and work. The usual fix is to configure each repo by hand — set user.email, maybe paste a token into the remote URL, run gh auth switch when the CLI complains.
It works until you forget. Then you get a commit authored by the wrong person, in the wrong repo, in front of the wrong people 🙃
Better idea: let the directory decide. Clone into ~/projects and you’re personal. Clone into ~/work and you’re work. Nothing to remember.
The Layout
Everything below assumes a split like this:
~/projects/ → personal account
~/work/ → work account
Pick whatever paths you already use. The rule is just: one tree per account.
Step 1: Log In to Both Accounts Once
The GitHub CLI holds multiple accounts for the same host at the same time. Log in twice:
gh auth login # personal
gh auth login # work
Check:
gh auth status
github.com
✓ Logged in to github.com account personal-username (keyring)
- Active account: true
✓ Logged in to github.com account work-username (keyring)
- Active account: false
Both tokens now live in your keychain. You can pull either one by name:
gh auth token -u work-username
That single command is what makes the rest of this work — no Personal Access Tokens to generate, store, or rotate by hand.
Step 2: Split Your Git Config by Directory
Git has a conditional include that most people never touch: includeIf. It loads an extra config file only when the repo lives under a given path.
Create one file per identity.
~/.gitconfig-personal:
[user]
name = personal-username
email = 12345678+personal-username@users.noreply.github.com
[credential "https://github.com"]
helper =
helper = "!f() { echo username=personal-username; echo password=$(gh auth token -u personal-username); }; f"
~/.gitconfig-work:
[user]
name = Your Full Name
email = you@company.com
[credential "https://github.com"]
helper =
helper = "!f() { echo username=work-username; echo password=$(gh auth token -u work-username); }; f"
Two things worth noticing:
- The empty
helper =line is not a typo. Git treats credential helpers as a list, and an empty value resets it. Without that line you’d inherit the global helper and it would answer first — with the wrong account. - The helper function just prints a username and a password. That’s the entire credential protocol. The password is fetched live from the keychain, so nothing sensitive is written to disk.
Now wire them into ~/.gitconfig:
[includeIf "gitdir:~/projects/"]
path = ~/.gitconfig-personal
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
⚠️ The trailing slash matters.
gitdir:~/projects/matches everything under that folder. Without the slash it matches only the folder itself.
Step 3: Remove the Global Identity
This is the step people skip, and then wonder why nothing changed.
git config --global --unset user.name
git config --global --unset user.email
Leave them set and you’ve got a silent fallback — any repo outside your two trees commits under whatever was there, instead of failing loudly.
While you’re at it, clear the per-repo overrides you set in the past. A local user.email beats includeIf every time:
for d in ~/projects/*/ ~/work/*/; do
[ -d "$d.git" ] || continue
git -C "$d" config --local --unset-all user.email 2>/dev/null
git -C "$d" config --local --unset-all user.name 2>/dev/null
done
Verify the whole set in one pass:
for d in ~/projects/*/ ~/work/*/; do
[ -d "$d.git" ] || continue
echo "$(basename $d): $(git -C $d config user.email)"
done
Every personal repo should report the personal email, every work repo the work one. If one disagrees, it still has a local override.
Step 4: Teach the gh CLI Too
includeIf governs Git. It does nothing for gh pr create or gh repo view — those follow the globally active account.
The fix is one shell hook. Add to ~/.zshrc:
_gh_account_chpwd() {
case "$PWD/" in
"$HOME"/projects/*) export GH_TOKEN="$(gh auth token -u personal-username 2>/dev/null)" ;;
"$HOME"/work/*) export GH_TOKEN="$(gh auth token -u work-username 2>/dev/null)" ;;
*) unset GH_TOKEN ;;
esac
}
autoload -Uz add-zsh-hook && add-zsh-hook chpwd _gh_account_chpwd
_gh_account_chpwd
chpwd fires on every directory change. GH_TOKEN overrides the active account, so gh follows your cd.
For bash, swap the hook for a PROMPT_COMMAND entry:
PROMPT_COMMAND="_gh_account_chpwd;${PROMPT_COMMAND}"
Reload and test:
source ~/.zshrc
cd ~/projects/anything && gh api user --jq .login # → personal-username
cd ~/work/anything && gh api user --jq .login # → work-username
What You Get
Clone a work repo:
cd ~/work
git clone https://github.com/company/service.git
cd service
No identity setup. No token in the URL. No gh auth switch. Commit and push — Git reads ~/.gitconfig-work, asks the helper for a token, and the helper hands over the work one.
Move the same repo to ~/projects and it becomes a personal repo. The config follows the path, not the clone.
Troubleshooting
Commits still show the old author. The repo has a local override. Run git config --list --local | grep user inside it.
Push asks for a password. The credential helper isn’t loading. From inside the repo:
git config --get-all credential.https://github.com.helper
You should see an empty first line, then your function. If the global helper shows up instead, your includeIf path doesn’t match — check the trailing slash, and remember that ~ expands but relative paths don’t.
gh uses the wrong account. echo $GH_TOKEN in a fresh shell. Empty inside one of your trees means the hook didn’t load or the case pattern doesn’t match your real paths.
Test a helper without pushing anything:
printf 'protocol=https\nhost=github.com\n\n' | git credential fill
Quick Reference
| Task | Command |
|---|---|
| List logged-in accounts | gh auth status |
| Get a specific account’s token | gh auth token -u USERNAME |
| Which identity applies here? | git config user.email |
| Which helper applies here? | git config --get-all credential.https://github.com.helper |
| Find local overrides | git config --list --local | grep user |
Check active gh account | gh api user --jq .login |
One Caveat
This is HTTPS-only. If you use SSH remotes, the same split works — point each includeIf file at a different key with core.sshCommand, or set up Host aliases in ~/.ssh/config. The directory logic doesn’t change, only the auth mechanism.
Set it up once and the wrong-account commit stops being a thing you can do 🎯