
When working with Git, you usually configure your user details (like name and email) globally. This means the same configuration applies to all repositories on your machine. However, there are situations where you might need different configurations for different directories. I didn’t have to worry about this because I used to dual-boot Windows and Ubuntu, so work and play were completely separate. However, due to some circumstances involving a failed M2 to SATA adapter, I now have to do everything from one setup.

As a result, I may or may not have committed code to a work repo with my personal email. I’m going to go over how not to do this.
In Git, configurations can be applied at three levels:
System: Configuration applies to all users and repositories on the system.
Global: Configuration applies to all repositories for the current user.
Local: Configuration applies only to a specific repository.
Separating Git configurations means setting different Git settings (such as username, email, signing key, etc.) for specific repositories or directories rather than using a single global configuration. This allows you to use different identities or settings depending on the project or the directory you work in.
There are various reasons why you might want to have different Git configurations for different directories:
Working with Multiple Accounts: If you are working on both personal and work-related projects, you may want to use separate Git configurations for each. For example, you may want to commit to your personal projects using your personal email, but use your work email for company-related repositories.
Different Signing Keys: Some projects might require you to sign your commits using a specific GPG key. This is common in open-source projects where verifiable commits are important.
Custom Behaviors for Specific Projects: You may want to enforce specific Git behaviors (e.g., using a specific diff tool or merge strategy) only for certain directories, such as for large repositories with unique requirements.
Identity Control: You can use different usernames and emails for work and personal projects, ensuring that your commits are always attributed to the correct account.
Project-Specific Settings: You can tailor Git's behavior for each repository, setting up preferences like commit signing, diff tools, or line-ending rules.
Security: For sensitive projects, you may want to apply stricter rules like requiring GPG signing for each commit.
Efficiency: Automating configuration means you won't need to remember to manually switch identities or settings before making a commit.
Before making any changes, it's a good idea to see what configurations you already have globally set up. Use the following command to view your global configuration:
git config --global --list
This will show you the current user name, email, and other settings applied to all repositories.
To set a configuration that only applies to a single repository, navigate to that repository and use the git config command with the --local flag. For example, to set a custom username and email:
cd /path/to/repository
git config user.name "Your Work Name"
git config user.email "your.work.email@example.com"
This will save the user name and email for that specific repository only, without affecting any other repositories.
If you want to apply a Git configuration to all repositories within a certain directory, you can use conditional includes. This feature allows you to create configuration rules based on the directory structure.
Start by editing your global .gitconfig file:
nano ~/.gitconfig
Now, add the following to conditionally include configurations based on the directory:
[includeIf "gitdir:~/path/to/work/"]
path = ~/path/to/.gitconfig-work
[includeIf "gitdir:~/path/to/personal/"]
path = ~/path/to/.gitconfig-personal
This example ensures that:
Repositories under ~/path/to/work/ will use a separate configuration from ~/path/to/.gitconfig-work.
Repositories under ~/path/to/personal/ will use the configuration from ~/path/to/.gitconfig-personal.
Next, create the respective .gitconfig-work and .gitconfig-personal files with your specific settings:
For ~/.gitconfig-work:
[user]
name = "Your Work Name"
email = "your.work.email@example.com"
For ~/.gitconfig-personal:
[user]
name = "Your Personal Name"
email = "your.personal.email@example.com"
Now, any repository within the defined directories will automatically use the respective configurations.
When working with different Git configurations, it’s often helpful to also use different SSH keys for authentication, especially if you’re using different Git services (e.g., GitHub for personal and GitLab for work).
Generate a new SSH key for each account:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
Add the new SSH key to your SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_work
Update your SSH config to use the appropriate key for each service. Open the ~/.ssh/config file and add entries like the following:
# Personal GitHub account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
# Work GitHub account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
When cloning repositories, you would use the appropriate host (e.g., github.com-personal or github.com-work), and Git will use the correct key for authentication.
Finally, make a test commit to ensure that the correct configuration is being used in each directory:
git commit --allow-empty -m "Test commit"
git log -1
Check the output to see if the correct user name and email were applied.
Separating Git configurations for different directories is a useful practice, especially for developers who juggle multiple identities, projects, or repositories. It provides more flexibility and ensures that your commits are always associated with the correct identity and settings. By using local configurations and conditional includes, you can easily manage multiple accounts and project-specific Git preferences.
With the steps outlined above, you should be able to configure Git to better suit your development workflow. Happy coding!
A passionate developer with 5+ years of experience in web development. Specializing in React, TypeScript, and modern JavaScript frameworks.
View all posts by Prince ShammahNo related articles found.
Get notified when new articles are published.