Qvantor logo QvantorInfrastructure, explained plainly
Dev Tools

Git includeIf Setup for Work and Personal Identities

The commit that made me fix this properly had my personal address on it, sitting in a client repository, pushed to a branch that about 30 people could see. Nothing terrible happened.

A laptop screen showing a gitconfig file with a conditional include section beside a terminal printing a configured email address

The commit that made me fix this properly had my personal address on it, sitting in a client repository, pushed to a branch that about 30 people could see. Nothing terrible happened. It just looked careless, and it took a force push and an awkward message in a channel to clean up.

The reverse is worse and more common. People push to a public open source project using the email their employer issued them, which quietly attaches a company name to an opinion in a pull request thread, and that address stays in the git history forever. I have seen someone discover this two years later while job hunting.

Why a single global identity always leaks eventually

Git has no concept of who you are beyond two strings in a config file. If your home directory config says your name and address, every repository on the machine inherits them unless something overrides it. There is no prompt, no warning, and no indication in the normal commit output that the identity might be wrong for this project.

The usual workaround is to set the identity per repository right after cloning. That works for exactly as long as you remember to do it. I kept notes on my own slips for a quarter and found four repositories where I had forgotten, out of roughly 40 clones. A ten percent failure rate on something that writes permanent metadata is not a workflow, it is a habit waiting to embarrass you.

Some people go further and remove the global identity entirely, so git refuses to commit until you set one locally. That does stop the wrong address, and it also stops you mid flow with an error on every fresh clone, which gets old by the third time in a morning.

The directory layout that does the real work

Conditional config is driven by where a repository lives on disk, so the layout comes first. Mine is three directories under home:

~/work/ for anything owned by an employer or client. ~/src/ for personal projects and anything public. ~/scratch/ for throwaway clones I expect to delete inside a week, which inherits the personal identity because nothing in there ever gets pushed.

If your repositories are scattered across your home directory with no pattern, do that part first. Twenty minutes of moving folders now saves the whole argument later. Git does not care how deep the nesting goes, only that the path has a predictable prefix.

Setting up the conditional include

The mechanism is a section header in your global config that only applies when the repository path matches. Open ~/.gitconfig in an editor rather than using the command line, because you are writing structure, not a single value.

Start with the personal identity as the default, then add the condition:

[user]
name = Your Name
email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work

Then create ~/.gitconfig-work with only the parts that differ:

[user]
email = [email protected]
[commit]
gpgsign = true

Three details decide whether this works or quietly does nothing. The trailing slash on the gitdir pattern matters, because without it the pattern matches a directory named exactly work and not its contents. The include has to come after the default values, since later entries win. And gitdir is case sensitive on Linux, so use gitdir/i if you are on macOS or Windows where the filesystem may hand git a path with different capitalisation than you expect.

To confirm it took effect, change into a repository under the work tree and ask git what it thinks your address is with git config user.email. Run the same command in a personal repository. If both return the same value, the pattern did not match.

The advice I would push back on

Plenty of guides stop at the email address and call the job done. I think that is the least important half of the setup.

An address in a commit is a label. The things that actually carry authority are your signing key and your SSH key, and if those are shared across contexts you have a real problem rather than a cosmetic one. A single personal SSH key with push access to both your side projects and your employer's monorepo means one compromised laptop account exposes both, and it means an offboarding process cannot revoke your work access without touching a key you still need.

So put the keys in the conditional file too. Add a signing key per identity, and set core.sshCommand in each config so git reaches for the right private key regardless of what your SSH agent happens to have loaded:

[core]
sshCommand = ssh -i ~/.ssh/id_work -o IdentitiesOnly=yes

The IdentitiesOnly option is the part people skip. Without it, the agent offers every key it holds and the server accepts the first one that works, which may not be the one you named. If you prefer to keep this in your SSH configuration instead, the per host approach in the SSH config file setup does the same job and is easier to reason about when you have several hosts.

For signing, the identity and the key have to agree or the commit shows as unverified. Set user.signingkey inside each conditional file next to the matching address. The mechanics of using an SSH key rather than GPG for this are covered in signing git commits with an SSH key, and it is the simpler route if you are starting fresh.

Checking what you already pushed

Once the setup is in place, spend ten minutes finding out how bad the existing situation is. In each repository, run git log and look at the author addresses on your own recent commits. Most hosting platforms also let you filter commits by author email, which is faster across many repositories.

Rewriting history to fix old commits is almost never worth it on a shared branch. The cost lands on everyone who has to reset their local copy, and the old objects usually survive on the server anyway. What I do instead is add the leaked address to the private email list on my hosting account so it stops appearing in profile links, and leave the history alone.

The exception is a commit that exposes something genuinely sensitive, such as an internal hostname in a message. That is a different problem with a different fix, and it should be handled as an incident rather than a tidy up. A few more opinionated setups like this one sit under dev tools.

Total time for the whole thing was about 25 minutes for me, most of it spent moving folders. I have not sent a commit from the wrong identity since, which after four years of doing it manually feels like a reasonable return.

FAQ

Does includeIf work with a remote URL instead of a directory?

Yes. Newer git versions support hasconfig:remote.*.url, which matches on the remote rather than the path. It is useful when you cannot reorganise directories, though path matching is easier to debug.

What git version do I need?

Conditional includes with gitdir landed in git 2.13, so anything from the last several years is fine. The remote URL condition needs 2.36 or newer.

Can I use different names as well as different addresses?

You can set any config value in the included file, including user.name. Many people keep one name and vary only the address and keys, which is usually enough.

Why does my commit still show the old address?

Check for a local override first, since a value set inside the repository beats anything global. Also confirm the trailing slash on your pattern and that the include sits below the default user section.

ON
Obi Nwachukwu

Obi works on account security and explains phishing, passwords and access in plain terms.

More posts by Obi

More in Dev Tools

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven strcoding tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto