If you're a developer, chances are you spend most of your time in your terminal, and Git is your most trusted command. But as soon as you start freelancing or contributing to open-source projects, a common headache appears. How do you manage your work identity (your-work@company.com) and your personal identity (your-personal@email.com) without constantly switching configs?
Accidentally pushing a commit to a work repository with your personal email is a rite of passage many of us would rather skip. Fortunately, Git has a powerful, built-in feature to solve this problem elegantly.
Forget manual switching scripts and constant worry. Let's set up a "set it and forget it" system that automatically uses the right profile for the right project.
The Best Method: Automatic Profile Switching with Conditional Includes
The most robust solution is to tell Git to use a default (work) profile for everything, unless a project is located in a specific folder (e.g, ~/personal_projects). This is done using a feature called includeIf.
Step 1: Establish Your Global (Work) Identity
First, we'll configure your global settings to default to your work profile. This will be the identity Git uses for any repository outside of your personal folder.
Step 2: Create a Separate Config for Your Personal Identity
Next, we'll create a dedicated configuration file that contains only your personal details. This keeps your identities neatly separated.
Step 3: Tell Git When to Switch (The Magic Step!)
Here's where the magic happens. We'll edit your main .gitconfig file to add a conditional rule. This rule tells Git: "If the project I'm in is inside the ~/personal_projects/ directory, then you must also load the settings from the ~/.gitconfig-personal file."
First, make sure your personal projects directory exists and then add the conditional rule:
The settings in the personal config will override the global ones for any project in that folder. Simple and effective!
Step 4: Verify Your Setup
You can easily test that it's working correctly.
# Expected output: your-work@company.com
# Expected output: your-personal@email.com
Alternative: The Manual Switch Script
If you prefer a more hands-on approach, you can use a terminal script to switch your global Git identity manually.
Warning: This method is prone to human error. It’s easy to forget which profile is active and commit with the wrong identity.
Create and Run the Script
You can create an executable file named git-profile in /usr/local/bin and paste the shell script below into it.
12345678910111213141516171819#!/bin/shset -e# Usage: git-profile [work|personal]if [ "$1" = "work" ]; thengit config --global user.name "Your Name"git config --global user.email "your-work@company.com"echo "✅ Git identity switched to Work profile."elif [ "$1" = "personal" ]; thengit config --global user.name "Your Name"git config --global user.email "your-personal@email.com"echo "✅ Git identity switched to Personal profile."elseecho "Usage: git-profile [work|personal]"CURRENT_EMAIL=$(git config --global user.email)echo "Current global email is: $CURRENT_EMAIL"exit 1fi
You would then run' git-profile work' or' git-profile personal' to switch.
Conclusion
While a manual script works, automating your Git profiles with conditional includes is a far superior approach. It removes the mental overhead, prevents mistakes, and lets you focus on what truly matters: writing great code.
Happy coding!
