跳到正文
DEV Community· Urvin Sanghavi·· 1 天前AI 評分62

Claude Code 為何會以錯誤的 GitHub 帳戶推送,以及 3 分鐘修正方法

Why your AI agent pushes as the wrong GitHub account (and a 3-minute fix)

AI 導讀

作者説明 Claude Code 會因 Git 與 GitHub CLI 的帳戶設定彼此獨立而以錯誤帳戶推送,並推出按資料夾選用帳戶的 claude-gh-accounts 安裝程式。

正文

You open a client project, ask Claude Code to fix a bug and open a pull request. Two minutes later the PR is up, from your personal GitHub account, with your personal email on every commit.

If you use more than one GitHub account on the same computer (work, personal, a client or two), you've probably hit some version of this:

  • 😬 commits on a work repo signed with your personal email,
  • 🙈 git push failing with "Permission to acme/api.git denied to jane-dev",
  • 🤖 an AI agent opening PRs and issues as whichever account happened to be active.

I hit all three in the same week, so I dug into why it happens and built a small open-source fix. This post explains the problem, the pieces of git and the GitHub CLI that solve it, and the surprising gotchas along the way.

Why it happens: three separate identities

When Claude Code (or you) works in a repo, three different things decide which GitHub account is used, and they don't talk to each other:

What Decided by Typical default
Commit author (name and email) git's user.name / user.email One global value in ~/.gitconfig
Push and clone git's credential helper (HTTPS) or your SSH key Whatever your keychain or first SSH key says
PRs, issues, API calls (gh pr create) The GitHub CLI's active account The last account you logged in with

The GitHub CLI has supported several logged-in accounts since version 2.40, but gh auth switch changes the active account globally. Switch to work in one terminal and every other terminal, including a Claude Code session you started an hour ago, is suddenly work too. Two agents in two repos can't each have their own account.

What people usually try

  • SSH host aliases (git@github-work:acme/api.git): fixes pushes over SSH, but you have to rewrite every remote, and it does nothing for commit emails or gh.
  • includeIf for the email only: fixes the commit author, not pushes or PRs.
  • gh auth switch before every task: works until you forget once. And you will.
  • direnv + GH_TOKEN: close, but it's manual setup per directory, and it's easy to leak a token into your global environment, where it quietly overrides gh everywhere.

Each fixes one of the three identities. You need all three, and you need them to follow the folder you're in.

The idea: one folder per account

Put each account's projects in its own folder, for example ~/code/work and ~/code/personal, and make everything inside a folder use that folder's account. Git and the GitHub CLI already have every piece needed.

1. Commit identity: includeIf

Git can load extra config only for repositories under a path:

# ~/.gitconfig
[includeIf "gitdir/i:~/code/work/"]
    path = ~/.gitconfig-work
# ~/.gitconfig-work
[user]
    name = Jane Doe
    email = jane@acme.com

Every repo under ~/code/work/ now commits as jane@acme.com. (Mind the trailing slash in gitdir/i:~/code/work/. Without it, the rule silently never matches.)

2. HTTPS pushes: a credential helper that asks gh for the right token

The GitHub CLI can print the token of any logged-in account, not just the active one:

gh auth token --user jane-at-acme

So the per-folder config can include a tiny credential helper that hands git exactly that account's token:

# ~/.gitconfig-work
[credential "https://github.com"]
    helper =
    helper = "!f() { test \"$1\" = get && echo username=jane-at-acme && echo password=$(gh auth token --user jane-at-acme); }; f"

The empty helper = line clears any helpers defined earlier, such as your OS keychain, so only this one runs inside the folder. Tokens stay in gh's secure storage; nothing new is written to disk.

3. SSH pushes: core.sshCommand

For git@github.com: remotes you don't need host aliases at all. Git can choose the key per repository:

# ~/.gitconfig-work
[core]
    sshCommand = ssh -i ~/.ssh/claude-gh-work -o IdentitiesOnly=yes

Your remotes stay exactly as they are. Bonus: includeIf is applied during git clone too, so cloning into the folder already uses the right key.

4. Claude Code: a per-process GH_TOKEN

gh always prefers the GH_TOKEN environment variable over its active account. So instead of switching anything globally, a small claude shell function finds which account folder you're in and starts Claude Code with that account's token, for that process only:

claude() {
  # ...find the account whose folder contains $PWD...
  GH_TOKEN="$(gh auth token --user "$user")" command claude "$@"
}

Now gh pr create inside that Claude session runs as the folder's account, and a second session in another folder can use a different account at the same time.

The gotchas (the fun part)

Getting this right on macOS, Linux and Windows turned up some surprises:

  1. "Last one wins" in .gitconfig. gh auth login can add a global credential helper for github.com. If it lands below your includeIf lines, its empty helper = reset wipes out your per-folder helper and you're back to the active account. The fix is to keep the per-folder includeIf entries at the end of the file.
  2. Symlinks. Git matches includeIf folders by their real path. If ~/code is a symlink (common with Dropbox, and even macOS's own /var → /private/var), a rule written with the symlinked path never matches. Save the resolved path.
  3. The workflow permission. Pushing a change to .github/workflows/*.yml fails with "refusing to allow an OAuth App to create or update workflow … without workflow scope" unless the login included that permission. Ask for it at login time (gh auth login --scopes workflow) and the error never shows up.
  4. PowerShell variable names ignore case. My Windows installer stored its colors in $C, and a later line did $c = Get-Command claude. Same variable. Half the installer lost its colors. (There's now a test that fails if that ever comes back.)
  5. Windows searches the system PATH before yours. If Claude Code was installed for all users (for example with npm into C:\Program Files\nodejs), a claude.cmd wrapper in your user PATH is never found. A doskey shortcut beats every PATH, so Command Prompt defines one when it starts.

The tool: claude-gh-accounts

I packaged all of this into claude-gh-accounts, a free, MIT-licensed installer for macOS, Linux and Windows (PowerShell and Command Prompt):

# macOS / Linux
curl -fsSL https://github.com/us-urvin/claude-gh-accounts/releases/latest/download/install.sh | bash
# Windows PowerShell
irm https://github.com/us-urvin/claude-gh-accounts/releases/latest/download/install.ps1 | iex

Demo: the installer, then claude in two folders using two GitHub accounts

It installs anything missing (Git, GitHub CLI, Claude Code), suggests the GitHub accounts you're already logged in to, fills in your name and email from your GitHub profile, and optionally sets up an SSH key per account. Most answers are just Enter. Then:

~/code/work/api        $ claude    ➜  GitHub account: jane-at-acme
~/code/personal/blog   $ claude    ➜  GitHub account: jane-dev

Projects outside the account folders keep their usual settings. Re-running the installer remembers everything, so adding an account takes a few seconds.

Current limits, honestly: it's built for Claude Code in the terminal. In IDE extensions and the desktop app, git commits and pushes still use the right account, but Claude Code's own gh actions don't switch yet. Web and IDE support is next. It also covers github.com only, not GitHub Enterprise, for now.

Try it, break it, tell me

If you juggle multiple GitHub accounts, I'd love to hear whether this fixes it for you, and what's missing. Ideas already on the list: a doctor command that shows which account a folder will use, and picking the account from the repo's owner instead of the folder.

👉 github.com/us-urvin/claude-gh-accounts. A ⭐ helps other people find it.

來源:DEV Community · dev.to