Software

Oct 2, 2025

3 min read
Featured
Editor's Pick

How To Manage Work And Personal Git Profiles

How To Manage Work And Personal Git Profiles | Step-by-Step Guide to Handling Two Git Profiles

D

DDU

Author

How To Manage Work And Personal Git Profiles

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.

Bash
~$git config --global user.name "Your Name"
~$git config --global user.email "your-work@company.com"

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.

Bash
~$touch ~/.gitconfig-personal
~$git config --file ~/.gitconfig-personal user.name "Your Name"
~$git config --file ~/.gitconfig-personal user.email "your-personal@email.com"

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:

Bash
~$git config --global --add includeIf."gitdir:~/personal_projects/".path "~/.gitconfig-personal"

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.

Bash
~$cd ~
~$git config user.email
# Expected output: your-work@company.com
~$cd ~/personal_projects
~/personal_projects$git config user.email
# 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.

Bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh
set -e
# Usage: git-profile [work|personal]
if [ "$1" = "work" ]; then
git 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" ]; then
git config --global user.name "Your Name"
git config --global user.email "your-personal@email.com"
echo "✅ Git identity switched to Personal profile."
else
echo "Usage: git-profile [work|personal]"
CURRENT_EMAIL=$(git config --global user.email)
echo "Current global email is: $CURRENT_EMAIL"
exit 1
fi

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!

Git