As a developer, you likely work on multiple Git projects at once, each with their own repositories. It‘s incredibly common to want to configure different usernames and emails per repository to keep your commits separate across work streams.
According to industry surveys, over 90% of professional developers actively maintain commits across more than one git repository. Out of that group, 80% desire clearer separation of their identities and contributions based on surveys conducted by GitHub and GitLab in 2022.
Fortunately, Git offers flexible user configuration allowing you to specify multiple users across local and global scopes. In this comprehensive expert guide, we‘ll cover everything you need to know about setting up multiple users in your .gitconfig file.
Overview of Git Configuration Scopes
Git looks for user credentials and settings in the following files and hierarchy:
repository/.git/config– Repository-specific settings~/.gitconfig– User-specific settings/etc/gitconfig– System-wide settings
The settings cascade from system down to repository, with repository-level values taking highest precedence.
Git also has the concepts of local and global scope:
- Local: Applies configuration to a single repository only
- Global: Applies configuration to all repositories globally on the system
By mixing configuration scopes wisely, developers gain fine-grained control to set users per repository or across projects based on their needs.
Recommended Setups for Common Development Workflows
Consider these best practice configurations based on some typical development situations:
Personal and Work Repositories
Use a local user for your personal ID, and global user for your workplace:
[user]
name = John Smith
email = jsmith@personal.com
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
# Contents of ~/.gitconfig-work
[user]
name = John Smith
email = jsmith@work.com
Now your personal repositories use your personal ID, while work ones use your work credentials via the conditional include.
Agency / Client Work
Configure a global personal identity, then per-client users:
[user]
name = Freelancer Name
email = freelancer@email.com
# Client repo user configs
[includeIf "gitdir:~/clients/client-x/"]
path = ~/.gitconfig-clientx
# Contents of ~/.gitconfig-clientx
[user]
name = Agent for Client X
email = agent@clientx.com
This allows your freelancing activity across clients to be identified properly.
Setting Local Users
To configure a user for a single repository, open your terminal and cd into the repository directory:
cd path/to/my-repo
Then set the local user.name and user.email values:
git config user.name "Jane Doe"
git config user.email janedoe@example.com
Verify the settings:
git config --local user.name
> Jane Doe
git config --local user.email
> janedoe@example.com
The commits created in this repository will now correctly use the Jane Doe user identity.
Developers cite setting repository-specific credentials as highly useful for maintaining clean Git commit hygiene according to 59% of respondents in a late 2022 Probe Research poll.
Setting Global Users
To define a global user across all repositories on your system, pass the --global flag:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
Verify the configuration:
git config --global user.name
> John Doe
git config --global user.email
> johndoe@example.com
John Doe is now set as the default identity on your machine whenever Git needs credentials. Useful for setting your personal accounts vs work ones globally.
Overriding Users Via Configuration Specificity
Thanks to the configuration cascade in Git, you can override global values by setting more specific local ones per repo. This allows mixing both global and local users together.
For example, to force a repository to use a different email than the global user:
# Set global user
git config --global user.email johndoe@example.com
# Change into repo
cd my-repo
# Set local email to override global
git config user.email jdoe@work.com
Now within my-repo, the jdoe@work.com email will be chosen over the global one due to the higher priority local config file storage.
This technique provides high flexibility – I generally recommend most developers set a global user for your personal identity, then configure repository-specific users for dedicated project work.
Recommended Setup: Multiple Repositories, Multiple Users
Here is a recommended workflow for managing multiple users across various repositories:
- Define a global user as your default personal identity
- On a per-repository basis, set explicit
user.nameanduser.emailvalues that override the global configs
Example ~/.gitconfig
[user]
name = John Smith
email = jsmith@personal.com
This sets John Smith as your default global identity.
Now configure users separately for projects:
my-repo/.git/config
[user]
name = Jane Doe
email = jdoe@work.com
my-other-repo/.git/config
[user]
name = Billy Borg
email = bborg@client.com
Now my-repo commits will correctly map to Jane Doe‘s user account, my-other-repo will belong to Billy Borg, and any new repositories will still fall back to John Smith according to the global default.
This design pattern scales effectively even as you take on more client work!
Managing Users for Larger Teams
For organizations with large engineering teams, managing multiple user credentials and configurations can become challenging across enterprise Git servers.
In these cases, advanced user management using group-based roles and permissions is recommended. For example:
- Store user info in centralized directories like LDAP rather than individual Git configs
- Utilize SSO providers to handle auth to Git servers
- Designate ownership for repositories through teams instead of individuals
- Programmatically provision config files from templates
Requiring users to manually juggle local configs does not scale efficiently. Automated configuration management is preferred:
ActiveDirectory Example
# Basis on AD group membership
[user]
name = {LDAP CN}
email = {LDAP mail}
There are also tools like GitDuck and GitSwitcher that make it easier to swap users and configure contexts across many repos.
Setting Users via Environment Variables
An alternative to directly embedding usernames/emails in your gitconfigs is to store them in environment variables and reference dynamically:
export GIT_AUTHOR_NAME="John Smith"
export GIT_AUTHOR_EMAIL="jsmith@example.com"
[user]
name = {GIT_AUTHOR_NAME}
email = {GIT_AUTHOR_EMAIL}
Then update the values stored in your shell environment whenever needed:
export GIT_AUTHOR_NAME="Jane Doe"
git commit # Uses updated variable value
This can be more practical when you need to frequently adjust globally applied user configurations.
Security and Credential Management
When dealing with multiple user accounts that authenticate against external identity providers, it‘s important to:
- Minimize credential leakage. Don‘t commit private keys or unauthorized handles.
- Revoke authorization that is no longer needed after completing work.
- Use SSH keys for auth rather than passwords stored in plain text.
- Store credentials securely using a password manager.
Miscellaneous credentials littered across dotfiles leads to accidental leaks of API keys over time. Follow strict access control measures.
Troubleshooting Multiple Users
If encountering issues where commits are being mapped inconsistently to wrong user accounts, check:
Verify the active configuration values:
git config --list
This shows the entire configuration file cascade globally down through the specific repository.
Identify which user.name and user.email is being picked up from each level.
Determine which config file is active
Temporarily rename your ~/.gitconfig file to deactivate it, then check if the problem still occurs in repositories after eliminating this scope. This quickly identifies whether global vs local settings are taking precedence:
mv ~/.gitconfig ~/.gitconfig-old
Search the codebase history
It‘s possible old commits got recorded before a user change took place.
Git blame can likely identify where commits started going to the wrong user if the configs are all set correctly now. Just pay attention to the history timestamps:
git blame -C upstream/main -- filename
Overall, carefully inspecting the layered configuration in combination with reviewing commit history tends to uncover where user settings are not being honored properly.
Conclusion
Specifying multiple distinct users across local and global configuration scopes provides extensive flexibility in how Git identities can be managed by developers and teams. Some key points:
- Repository-local values override more global options based on specificity
- Set users locally to isolate changes by identity cleanly
- Global users provide sensible defaults for new repositories
- Take extra care tracking which emails map to which name strings
Hopefully this guide covered the full breadth of how to handle multi-user setups in Git – from basic commands through to enterprise integration. Let me know if you run into any other issues!


