When working with Git, your commits are tied to an identity defined by user.name and user.email. By default, these values are set globally for all repositories on your machine. However, there are cases where you might want to use different credentials for specific repositories.
Global vs. Local Configuration
Git allows you to configure settings at different levels:
Global Configuration: Applies to all repositories on your system. These settings are stored in ~/.gitconfig
(Linux/macOS) or C:\Users\%UserName%\.gitconfig
(Windows).
Local Configuration: Applies only to a specific repository. These settings are stored in the .git/config
file within that repository.
Local settings override global settings, meaning that if a repository has its own user.name and user.email, those will be used instead of the global ones.
Checking Your Current Git Identity
To see what identity Git is currently using, run:
|
|
This will display the global settings that apply to all repositories unless overridden. To check the repository-specific values, navigate to the repository folder and run:
|
|
Setting a Repository-Specific Git Identity
If you want to use a different name and email for a specific repository, navigate to the repository folder and run:
|
|
This updates the .git/config file inside the repository, applying only to that project.
Why Change Your Git Identity Locally?
There are several reasons you might want to override your global Git profile for a particular repository:
Work vs. Personal Projects: If you contribute to open-source projects using a personal email but commit to work repositories with your corporate email.
Multiple Git Accounts: If you use different GitHub, GitLab, or Bitbucket accounts, each requiring a separate email.
Project-Specific Branding: In team environments, some projects may require you to use a standardised author format.
Avoid Leaking Corporate Email: If your global configuration includes a work email but you’re contributing to a public repository, setting a repository-specific email prevents exposing your corporate identity.
Verifying the Configurations
You can check all configurations (global and local) using:
|
|
This will show global and local settings, making it easy to spot overrides.
Resetting to the Global Identity
If you no longer need a repository-specific identity, you can remove the local setting:
|
|
This will revert to the globally configured name and email.
Using GitHub Actions Bot Identity
If you’re using GitHub Actions and want commits to be attributed to the GitHub Actions bot, use the following values:
|
|
This ensures that commits made by your workflow are clearly identified as being from the GitHub Actions bot, helping with auditing and tracking automated changes.
Wrap Up
Customising user.name and user.email per repository is a simple yet effective way to manage multiple Git identities. Whether you’re separating work and personal commits, using different accounts, or safeguarding privacy, this approach gives you flexibility while maintaining clean commit histories.