This guide explains how to manage multiple Git identities (for example work and personal) using a simple bash script. The approach uses:
- Different Git global user configurations
- Different SSH identities
- A small script to switch profiles
This prevents committing with the wrong email when working across different repositories.
Git only supports one global identity at a time:
git config --global user.name
git config --global user.email
Developers who work with multiple accounts often accidentally commit using the wrong email address.
Example mistake:
Author: Developer Name <personal@email.com>
But the commit should have used the work email.
The script provides three commands:
| Command | Description |
|---|---|
| work | Switch to work Git profile |
| personal | Switch to personal Git profile |
| show | Display current Git configuration |
#!/bin/bash
# Git Profile Switcher Script
show_help() {
echo "Usage: $0 [work|personal|show]"
echo ""
echo "Commands:"
echo " work - Switch to work profile (github.com)"
echo " personal - Switch to personal profile (git@personal)"
echo " show - Show current git configuration"
}
set_work_profile() {
echo "Setting up work profile..."
git config --global user.name "Developer Name"
git config --global user.email "work@email.com"
echo "Work profile activated"
}
set_personal_profile() {
echo "Setting up personal profile..."
git config --global user.name "Developer Name"
git config --global user.email "personal@email.com"
echo "Personal profile activated"
}
show_current_config() {
echo "Current Git Configuration:"
echo "Name: $(git config --global user.name)"
echo "Email: $(git config --global user.email)"
}
case "$1" in
work)
set_work_profile
;;
personal)
set_personal_profile
;;
show)
show_current_config
;;
*)
show_help
;;
esacExample location:
~/scripts/git-profile
chmod +x ~/scripts/git-profile
Add this to .bashrc or .zshrc:
export PATH="$HOME/scripts:$PATH"
Reload the shell:
source ~/.bashrc
Now the command is available globally.
git-profile work
git-profile personal
git-profile show
Example output:
Current Git Configuration:
Name: Developer Name
Email: personal@email.com
The script assumes two SSH identities.
~/.ssh/id_ed25519
Used with repository URLs like:
git@github.com:company/repo.git
~/.ssh/id_rsa
Configured as a custom host in SSH.
Example SSH config (~/.ssh/config):
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
git@github.com:company/project.git
git@personal:username/project.git
Test which SSH identity is used.
ssh -T git@github.com
or
ssh -T git@personal
Expected output:
Hi username! You've successfully authenticated.
git-profile work
cd work-project
git commit
git push
git-profile personal
cd personal-project
git commit
git push
- Prevents wrong commit identity
- Fast profile switching
- Minimal setup
- Works with existing SSH configuration
- Easy to extend for more profiles