Sub topic 14
Separate identities for work and personal projects
You need Git installed, a working terminal, and a basic understanding of repositories and commits. You should also know the system, global and local scopes, and that a more specific scope overrides a broader one.
The problem with one global identity
You set user.name and user.email once with --global. That works until you also commit to work projects. Your personal email then appears in company history, or your work email appears in your open-source commits.
You can fix this per repository with git config --local:
cd my-project
git config --local user.email "[email protected]"This is easy to forget. Every new clone starts with the global identity, so you must repeat the step each time. Conditional includes remove that chore.
Conditional includes with includeIf
An includeIf section in a config file loads another file only when a condition is true. The most common condition is gitdir:, which matches repositories whose .git directory is under a given path.
Put this in ~/.gitconfig:
[user]
name = Ada Lovelace
email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-workThen create ~/.gitconfig-work:
[user]
email = [email protected]Any repository under ~/work/ now uses [email protected]. Everything else keeps the global email. The name still comes from the global file, because the work file does not set it.
How Git resolves the value
Git reads the global file from top to bottom. When it reaches the includeIf line and the condition matches, it reads the included file at that point, as if its contents were pasted there. Later values override earlier ones, so the included email replaces the one above it.
The local file .git/config is read after the global file, so a --local setting still wins over an include.
Rules that cause most mistakes
- Put the trailing slash in
gitdir:~/work/. With it, the pattern matches everything inside the directory. Without it, the pattern matches only that exact path. - The condition applies to the repository's location, not your current directory. It has no effect outside a repository.
- The
includeIfline must come before the settings you want to override. If you put it above[user], the global email replaces the included one. - Use
gitdir/i:instead ofgitdir:for a case-insensitive match, which helps on Windows and macOS.
Verify it
Create a repository in each location and ask Git where the email comes from:
mkdir -p ~/work/demo ~/play/demo
cd ~/work/demo && git init && git config --show-origin --get user.email
cd ~/play/demo && git init && git config --show-origin --get user.emailThe first command should print [email protected] with the path of ~/.gitconfig-work. The second should print [email protected] with ~/.gitconfig. If the work repository still shows the global email, check the trailing slash, the order of the sections, and that you ran the command inside the repository.
Exercise
Your ~/.gitconfig has [includeIf "gitdir:~/work"] without a trailing slash, and repositories under ~/work/ still show your personal email. What is the likely cause, and how do you fix it?
Show answer
Without the trailing slash, the pattern matches only the directory named ~/work itself, not the repositories inside it. Change it to [includeIf "gitdir:~/work/"]. Then confirm with git config --show-origin --get user.email inside a work repository.
Exercise
A work repository has git config --local user.email "[email protected]" set. Your includeIf supplies [email protected]. Which email do new commits use?
Show answer
[email protected]. The local file .git/config is read after the global file and its includes, so the local value wins. Remove it with git config --local --unset user.email to let the include apply.