Git Set local user.name and user.email different for each repo
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Git lets you set user.name and user.email at the repository level, so one machine can use different identities for work, open source, or personal projects. The important part is understanding that local configuration lives inside the repository’s .git/config file and overrides your global defaults only for that repo.
Git Configuration Precedence
Git reads configuration from multiple places, with more specific settings overriding broader ones. For this topic, the relevant levels are:
- system
- global
- local
Local configuration applies only to the current repository. That is what you want when one repo must commit as [email protected] and another must commit as [email protected].
Set a Different Identity for One Repository
Open a shell inside the repository and run:
Because the command is being run inside a Git repository, Git writes these values into the local repository config by default. You can make that explicit if you want:
After that, new commits in this repository use the local identity instead of the global one.
Verify Which Value Git Will Use
When debugging identity issues, do not guess. Ask Git which value it sees and where it came from:
To inspect all matching user settings:
This is useful when you suspect a global config, include file, or repo-local override is not what you expected.
Confirm with a Test Commit
You can verify the active author settings before pushing anything important:
If the output shows the expected name and email, the repository is configured correctly. If not, inspect the local config again before continuing.
What File Git Changes
Repository-specific settings are stored in:
A minimal example looks like this:
You normally do not need to edit that file by hand. git config is safer because it preserves formatting and avoids small syntax mistakes.
Local Config Changes Future Commits, Not History
Changing user.name and user.email locally affects future commits only. Existing commits already contain author and committer metadata inside the commit objects. If earlier commits used the wrong identity, fixing the config now will not rewrite them.
For future work, this is enough. For old commits, you would need history rewriting, which is a separate and more invasive operation.
A Good Default Setup
Many developers keep a general personal identity globally and override it only in the repositories that require a different one:
Then, inside work repositories:
That setup is simple and predictable. It also makes mistakes obvious, because only the special repositories need an override.
Common Pitfalls
The most common mistake is running git config --global when the intention was to change only one repository. That changes the default for every repo on the machine.
Another pitfall is running git config user.name ... outside a Git repository and assuming the current project was updated. If there is no repo context, Git cannot write a local repo config.
It is also easy to forget that existing commits keep their original identity. Changing the config fixes only new commits, not old history.
Finally, do not rely on memory when debugging. Use git config --show-origin so you can see exactly which file is supplying the active value.
Summary
- Set per-repo identities with
git config user.nameandgit config user.emailinside the repository. - Local config overrides global config only for that repository.
- Verify the active settings with
git config --show-origin. - Repository-local values live in
.git/config. - Config changes affect future commits, not commits that already exist in history.
Related reading
- Git short branch name in teamcity
- git stash apply version
- git stash apply version
- git stash changes apply to new branch?
- Git stash uncached how to put away all unstaged changes?
- Git Stash vs Shelve in IntelliJ IDEA
- ''git status'' shows changed files, but ''git diff'' doesn''t
- Git status shows files as changed even though contents are the same
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.