Split-Brain Git and AI Agents: Solving the Identity and Token Chaos in Worktrees
Corporate work by day, open-source projects by night. When Git worktrees and autonomous AI agents enter the mix, commit identities and GitHub tokens clash. Here is how to fix it permanently using includeIf and dynamic credential helpers.
Corporate repositories by day, personal open-source projects by night; running across dozens of parallel Git worktrees and autonomous AI agents.
Using multiple GitHub accounts on a single development machine sounds trivial on paper: alice-work for daytime enterprise deliverables, and bob-personal for evening side projects and open-source contributions.
However, once Git Worktrees and autonomous AI coding agents become core parts of your daily workflow, things quietly derail.
Before you know it, an automated agent commits to a corporate repository using your personal email. Hours later, you attempt to push to your side project, only to face 403 Forbidden or Repository not found errors because gh auth is still anchored to your daytime identity.
In this post, we look at why per-repo configs and manual gh auth switch calls fall short, and how to build a fully directory-isolated (includeIf) and frictionless dual-identity setup.
Anatomy of the Failure: Why Repo Configs and AI Agents Clash
The traditional recommendation is either configuring user.name / user.email manually per repository or keeping a single global fallback. This model breaks across three specific seams:
~/.gitconfig (Global)
├── user: bob@personal.dev (bob-personal)
└── credential.helper: gh auth git-credential (currently active: alice-work)
1. Worktree Detachment
When juggling concurrent tasks or spawning AI agents, running git worktree add is standard practice. But in a worktree, .git is not a full directory; it is a pointer file (gitdir: ...) referencing the main repository metadata.
If the base repository omitted a local user.email, Git instantly falls back to your global ~/.gitconfig. Suddenly, your personal commit identity ends up on a corporate pull request.
2. The GitHub CLI (gh) Single-Active Token Trap
By default, GitHub CLI configures Git credential management like this:
[credential "https://github.com"]
helper = !gh auth git-credential
gh auth git-credential only knows one active account (Active: true). If alice-work was active during the day, any evening push to a bob-personal private repo sends Alice’s token over HTTPS. GitHub responds with an immediate fatal: repository not found.
3. Non-Deterministic AI Agents
Autonomous agents (Claude Code, Cursor, subagent task runners) execute Git commands programmatically. Without an unyielding, path-based rule, they resolve credentials and author signatures inconsistently depending on working directories and environment variable inheritance.
The Architecture: Strict Directory-Level Isolation
The core principle: “If Git knows where it is located on disk, it knows who it should be.”
Organize projects under explicit root hierarchies:
~/workspace/
├── work/ # Enterprise repositories (alice-work / alice@company.com)
└── personal/ # Personal / open-source (bob-personal / bob@personal.dev)
Regardless of whether an agent creates 15 ad-hoc worktrees or clones fresh modules inside these trees, the commit identity and authentication token should bind automatically to that directory prefix.
Step-by-Step Implementation
Step 1: Corporate Profile (~/.gitconfig-work)
Create a scoped Git profile for enterprise projects:
# ~/.gitconfig-work
[user]
name = Alice Smith
email = alice@company.com
# ALWAYS use alice-work credentials for GitHub inside this path:
[credential "https://github.com"]
helper =
helper = !gh auth token -u alice-work
Why the empty
helper =line? Git processes credential helpers as an execution chain. An empty value unsets any previous global helpers, ensuring Git exclusively queriesgh auth token -u alice-work. This extracts the valid token directly from the system keychain—completely decoupled from whatever account is marked active ingh.
Step 2: Personal Profile (~/.gitconfig-personal)
Create the corresponding profile for personal work:
# ~/.gitconfig-personal
[user]
name = Bob Jones
email = bob@personal.dev
# ALWAYS use bob-personal credentials for GitHub inside this path:
[credential "https://github.com"]
helper =
helper = !gh auth token -u bob-personal
Step 3: Wire Directory Matching with includeIf
Edit your global ~/.gitconfig and wire the scoped files using Git’s native conditional includes:
# ~/.gitconfig
[includeIf "gitdir:~/workspace/work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/workspace/personal/"]
path = ~/.gitconfig-personal
⚠️ Trailing Slash Requirement: The trailing
/ingitdir:~/workspace/work/is mandatory. It instructs Git to treat the pattern as a prefix match, covering every child repository and nested worktree.
Why Worktrees Stay Immune
Introduced in Git 2.13, includeIf "gitdir:..." evaluates against the real filesystem location of the checked-out worktree.
Consider:
- Main repository:
~/workspace/work/payment-service - Worktree generated by an AI agent:
~/workspace/work/payment-service-sc-1234
When an agent commits or pushes from inside payment-service-sc-1234:
- Git inspects the active working directory and matches the
~/workspace/work/prefix. - Git loads
~/.gitconfig-work. - Commit authorship defaults to
Alice Smith <alice@company.com>. - HTTPS operations fetch Alice’s token on the fly via
gh auth token -u alice-work.
The agent requires zero custom environment flags or manual switches.
Verification
Validate the behavior across workspaces:
# 1. Verify author resolution inside a corporate worktree:
git -C ~/workspace/work/payment-service-sc-1234 config user.email
# Output: alice@company.com
# 2. Verify author resolution inside a personal repository:
git -C ~/workspace/personal/my-side-project config user.email
# Output: bob@personal.dev
# 3. Test remote operations without running 'gh auth switch':
git -C ~/workspace/personal/my-side-project ls-remote
# Successfully authenticates via bob-personal even while alice-work is active globally!
Summary
| Challenge | Per-Repo Manual Config | includeIf + Dynamic Token Binding |
|---|---|---|
| Commit Identity | Brittle; requires manual git config per clone | 100% deterministic per root directory |
| Worktree Compatibility | Missed easily on new worktree setups | Inherited immediately via filesystem path |
| Remote Auth (Push/Fetch) | Relies on manual gh auth switch | Scoped via gh auth token -u <account> |
| AI Agent Safety | Prone to credential and author leaks | Zero-configuration, fail-safe isolation |
With directory-scoped identities, switching between corporate tasks and open-source hacking—or running parallel agent sessions across multiple worktrees—no longer risks identity contamination or broken authentication pipes.