Skip to content

Instantly share code, notes, and snippets.

@AHS12
Last active March 16, 2026 05:35
Show Gist options
  • Select an option

  • Save AHS12/e97eb824267db6bb0d5f95b39bde99b3 to your computer and use it in GitHub Desktop.

Select an option

Save AHS12/e97eb824267db6bb0d5f95b39bde99b3 to your computer and use it in GitHub Desktop.
github multiple profile switch in same device

Git Profile Switcher Guide

Overview

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.


1. Problem

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.


2. Script Capabilities

The script provides three commands:

Command Description
work Switch to work Git profile
personal Switch to personal Git profile
show Display current Git configuration

3. Script

#!/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
        ;;
esac

4. Installation

Save the script

Example location:

~/scripts/git-profile

Make it executable

chmod +x ~/scripts/git-profile

Add to PATH

Add this to .bashrc or .zshrc:

export PATH="$HOME/scripts:$PATH"

Reload the shell:

source ~/.bashrc

Now the command is available globally.


5. Usage

Switch to Work Profile

git-profile work

Switch to Personal Profile

git-profile personal

Show Current Configuration

git-profile show

Example output:

Current Git Configuration:
Name: Developer Name
Email: personal@email.com

6. SSH Setup

The script assumes two SSH identities.

Work key

~/.ssh/id_ed25519

Used with repository URLs like:

git@github.com:company/repo.git

Personal key

~/.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

7. Repository URL Examples

Work repository

git@github.com:company/project.git

Personal repository

git@personal:username/project.git

8. Verify Authentication

Test which SSH identity is used.

ssh -T git@github.com

or

ssh -T git@personal

Expected output:

Hi username! You've successfully authenticated.

9. Typical Workflow

Work repositories

git-profile work
cd work-project
git commit
git push

Personal repositories

git-profile personal
cd personal-project
git commit
git push

10. Benefits

  • Prevents wrong commit identity
  • Fast profile switching
  • Minimal setup
  • Works with existing SSH configuration
  • Easy to extend for more profiles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment