Commit with multiple Git accounts automatically using includeIf
git config --global user.email sets one identity for every repo on the machine. That’s fine until you push to more than one platform, or more than one account on the same platform.
The problem
By default ~/.gitconfig looks like this:
[user]
name = Christos Monogios
email = [email protected]
It applies everywhere, no matter which remote a repo pushes to. Fine with one account, not once you also want, say, a bot committing to a GitHub repo under its own name.
The fix: includeIf
Git can load a different config file depending on where the repo sits on disk:
[user]
name = Christos Monogios
email = [email protected]
[includeIf "gitdir:C:/<path-in-your-computer>/bitbucket/"]
path = C:/<path-in-your-computer>/.gitconfig-bitbucket
[includeIf "gitdir:C:/<path-in-your-computer>/github/personal/"]
path = C:/<path-in-your-computer>/.gitconfig-github-personal
[includeIf "gitdir:C:/<path-in-your-computer>/github/bot/"]
path = C:/<path-in-your-computer>/.gitconfig-github
Each included file overrides [user], and can set credential.username too, which becomes relevant in the next section:
# .gitconfig-github
[user]
name = auto-commit
email = [email protected]
[credential]
username = auto-commit
Clone or init a repo into the right folder and git config user.name already resolves correctly. Nothing to remember per repo, the folder decides.
Giving each account its own credential
[user] only decides the commit author. Authentication is a separate job, handled by the credential helper, which stores one entry per host. Setting up a second account on the same host takes a few steps:
-
Check what’s already stored. Mine looked like this before I touched anything, one entry per host:
Target: git:https://bitbucket.org User: ChristosMonogios Target: git:https://github.com User: ChristosMonogios -
Set
credential.usernamein the folder’s included config (already shown above), instead of editing every remote URL by hand. Any repo cloned into that folder now asks the credential helper forauto-commit’s entry automatically. One thing that overrides this silently: a username already baked into the remote URL wins overcredential.username, so a repo cloned before this setup existed needsgit remote set-url origin https://github.com/<owner>/<repo>.git(no username in it) or the old one keeps getting used. -
Store the matching credential. On Windows, with
cmdkey:cmdkey /generic:"git:https://[email protected]" /user:auto-commit /pass:"<personal access token>"On Linux it depends on
credential.helper.storewrites a plain-text~/.git-credentials, one line per URL (https://auto-commit:<token>@github.com), simplest option but the token sits unencrypted on disk. Setting it inside the bot’s included file adds to whatever helper is already configured globally rather than replacing it (git config --get-all credential.helperwill show both) — to makestorethe only one in effect there, clear the inherited value first:[credential] helper = helper = storemanageris the same Git Credential Manager used on Windows, and withcredential.usernameset throughincludeIfit usually resolves the stored token directly, no prompt.One exception: GitHub and Bitbucket both have an OAuth-aware provider inside GCM that caches one session per host, not per account, so a second account keeps re-prompting its own sign-in window regardless of
credential.username. Skip that provider and use the stored token directly:[credential "https://github.com"] provider = genericin the bot’s included file, so only that account is affected, not your personal one.
-
Give the account write access to the repo (Settings → Collaborators & teams). The credential only supplies a token, GitHub resolves it server-side to whichever account generated it, and checks that account’s permissions same as any other push.
Overriding a single repo
includeIf only sets the default. git config user.email ... run inside one repo still wins over it, same as it always did, in case a one-off repo needs to differ from its folder.
What’s actually verified
git log -1 confirms the author swaps automatically per folder. git credential fill confirms the matching token comes back automatically too, no username typed anywhere, no OAuth prompt. The one piece left outside Git entirely is auto-commit itself, a real account invited to the repo with write access, same as any other collaborator.
Feel free to drop me a comment, if you found this article helpful.