Git Config Setup for Multiple Environments
At my new job, I need to commit using my real name, while I use a secondary identity for my open-source projects. This article documents how to achieve multi-environment identity compatibility using Git’s `includeIf` configuration mechanism without affecting your usual settings.
- 3 min read
- 2026.08.04
Git’s `includeIf` can automatically switch the commit identity based on the repository path: Split your company and open-source `user.name` and `user.email` into separate configuration files, then include them in `~/.gitconfig` using `includeIf "gitdir:path/"`. This way, the settings take effect automatically when you enter the corresponding directory, eliminating the need to manually change them each time.
Git Config Setup for Multiple Environments
🧭 Background
When using Git across multiple development scenarios, we often encounter conflicts regarding identity information:
Manually switching between user.name and user.email every time is not only cumbersome but also prone to errors. Is there a way to automatically switch Git configurations?
The answer is—yes, using includeIf !
❓ Why Distinguish Between Git Identity Configurations?
Here’s how I use it personally:
- Company project commits: Use my Chinese name + company email
- Open-source project commits: Use my alias
Cassian Florin
- GitHub email
If you don’t need to manage multiple identities, the --global configuration is actually sufficient:
git config --global user.email "公司邮箱"
git config --global user.name "中文名"
git config --global pull.rebase true
🧬 A More Flexible Multi-Identity Configuration (Recommended)
✍️ My Development Environment
- macOS 15.4.1 (M1 Pro)
- JetBrains Toolbox suite (CLion, WebStorm, DataGrip)
- Git installed globally
- The article directory structure uses
~/dev/as the main directory
📁 Create an Additional Git Configuration File
- Open Terminal and navigate to your home directory:
cd ~
ls -a
- Create a new Git configuration file:
vim .gitconfig_qnvip
Sample content:
[user]
name = Cassian Florin
email = your-open-source-email@example.com
You can also use touch .gitconfig_qnvip to create an empty file first and then edit it.
🔁 Modify the .gitconfig
main file to enable conditional switching
Edit ~/.gitconfig and add the following configuration:
[user]
name = 中文名
email = 公司邮箱
[core]
autocrlf = input
[pull]
rebase = true
[includeIf "gitdir:/Users/huapai/dev/open/"]
path = /Users/huapai/.gitconfig_qnvip
🧪 Practical Demo
Initialize a new project in the ~/dev/open/ directory:
mkdir ~/dev/open/test-project
cd ~/dev/open/test-project
git init
View the currently active Git configuration:
git config user.name
git config user.email
If Cassian Florin and your open-source email address are displayed, it means includeIf is active!
✅ Summary & Recommendations
- If you’re a developer with only one identity, using global configuration is sufficient.
- If you have company, open-source, or multiple roles, we recommend using
includeIfto manage multiple identities. - Later on, you can also write automation scripts, switch identities with a single click, or use Git hooks in conjunction with this setup.