Git Multi-Repository User Identity Auto-Switching and Privacy Protection Guide


In daily development, many developers work with multiple Git repositories simultaneously, such as:

Different repositories may require different commit identities (user.name / user.email). Additionally, to protect personal privacy, you may want to hide your real email when committing to public repositories. This article systematically explains how to use Git’s includeIf functionality to achieve automatic user identity switching, support HTTPS/SSH, and hide email information.


1. Background and Goals

We want to achieve the following goals:

  1. Automatic Identity Switching: Automatically select corresponding user.name and user.email based on remote repository
  2. Multi-Protocol Support: Both HTTPS and SSH can be used
  3. Privacy Protection: Use GitHub noreply email for public repositories
  4. Unified Multi-Repository Management: No need to modify configuration in each repository separately

2. Git Version Requirements

This article uses Git 2.49.0 (macOS) for examples.


3. Configuration Approach

Git provides includeIf functionality that can automatically include other configuration files based on conditions.

Core Concept:


4. Configuration Examples

4.1 Global Configuration File ~/.gitconfig

# ----------------------------
# Global defaults
# ----------------------------

[user]
name = example
email = example@dev.com

[init]
defaultBranch = main

[credential]
helper = osxkeychain

# ----------------------------
# Git LFS support
# ----------------------------

[filter "lfs"]
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true

# ----------------------------
# IncludeIf: GitHub
# ----------------------------

# HTTPS
[includeIf "hasconfig:remote.origin.url:https://github.com/**"]
path = /Users/example/.gitconfig-github

# SSH
[includeIf "hasconfig:remote.origin.url:git@github.com:**/***"]
path = /Users/example/.gitconfig-github

# ----------------------------
# IncludeIf: Corporate Intranet GitLab
# ----------------------------

# HTTPS
[includeIf "hasconfig:remote.origin.url:https://work-ip-address/**"]
path = /Users/example/.gitconfig-work

# SSH
[includeIf "hasconfig:remote.origin.url:git@work-ip-address:**/**"]
path = /Users/example/.gitconfig-work

Notes:


4.2 GitHub Sub-Configuration File ~/.gitconfig-github

[user]
name = Example
email = example@users.noreply.github.com

4.3 Corporate Intranet Sub-Configuration File ~/.gitconfig-work

[user]
name = Work
email = work@company.com

5. Configuration Verification

  1. Remove local repository override configuration:
git config --unset user.name
git config --unset user.email
  1. Test current repository identity:
git remote -v
git config --show-origin user.name
git config --show-origin user.email

6. Additional Tips

6.1 HTTPS and SSH

https://github.com/** # HTTPS
git@github.com:**/** # SSH

6.2 Hide User Information

6.3 Historical Commit Privacy


7. Summary

Through Git’s includeIf functionality, we can:

This configuration is especially suitable for individual developers and open source contributors who simultaneously manage corporate intranet repositories and GitHub public repositories.