Skip to content

Dheeraj23qw/git-multiple-accounts-windows

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

One Machine, Two Identities: The Git & SSH Mastery Guide

If you have a Work GitHub and a Personal GitHub on the same Windows laptop, you know the headache.
This guide explains how to make your computer automatically switch identities so you never commit work code with your personal email again.


The Struggle (The "Why")

Most developers start with one GitHub account. But when you get a job, you get a second one. Suddenly:

  • Identity Crisis: You commit code to your office repo, but it shows your personal email.
  • Permission Denied: GitHub gets confused about which SSH key belongs to which account.
  • Manual Pain: You find yourself typing git config user.email "..." every single time.


Phase 1: Creating Your Digital IDs (SSH Keys)

Think of SSH keys as your digital fingerprints. You need a unique one for each account.

# Create your Personal Key
ssh-keygen -t ed25519 -f ~/.ssh/id_self

# Create your Work Key
ssh-keygen -t ed25519 -f ~/.ssh/id_work
Note: You will get two files for each: a Private Key (keep it secret!) and a Public Key (.pub). Copy the content of the .pub files and paste them into your respective GitHub account settings.

Phase 2: The "Key Manager" (SSH Agent)

Your computer is forgetful. You must "hand" your keys to a manager (the SSH Agent) so it can show them to GitHub when you push code.

# 1. Start the Manager
eval $(ssh-agent -s)

# 2. Give the keys to the Manager
ssh-add ~/.ssh/id_self
ssh-add ~/.ssh/id_work


Phase 3: The "Translator" (SSH Config)

GitHub's address is always github.com. We need to create "nicknames" so our computer knows which key to use.

Open or create ~/.ssh/config and paste this:

# Personal Alias
Host github.com-self
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_self
    IdentitiesOnly yes

# Work Alias
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_work
    IdentitiesOnly yes

Phase 4: The "Auto-Switcher" (Git Config)

This is the magic part. We tell Git: "If I am in my Work folder, use my Work email. Otherwise, use my Personal email."

In your main ~/.gitconfig file:

[user]
    name = Your Name
    email = personal@email.com

# THE MAGIC RULES
[includeIf "gitdir/i:C:/Users/rahul/work/"]
    path = ~/.gitconfig-work

[includeIf "gitdir/i:C:/Users/rahul/personal/"]
    path = ~/.gitconfig-personal
Crucial Point: We use gitdir/i: because Windows is case-insensitive (it doesn't care about C: vs c:). The / at the end tells Git to include everything inside that folder.

Creating Your Sub-Profile Configuration Files

For the conditional rules above to work properly, you must create the matching text profile files in your home directory:

Create ~/.gitconfig-personal and paste:

[user]
    email = dheeraj9508820247@gmail.com

Create ~/.gitconfig-work and paste:

[user]
    email = codewithdheeraj19@gmail.com

Phase 5: Testing Your Success

Run these commands to see if the computer says "Hi" to the right person!

Action Command Expected Result
Test Personal ssh -T git@github.com-self "Hi PersonalUsername!"
Test Work ssh -T git@github.com-work "Hi WorkUsername!"
Verify Email git config user.email The correct email for that folder

The Golden Rule for Daily Life

When you clone a new project, always use your nickname alias. If you don't, Git won't know which key to use!

Bad way: git clone git@github.com:org/repo.git

Good way: git clone git@github.com-work:org/repo.git


The "All-Commands" Git & SSH Cheat Sheet

A comprehensive list of every command required to set up and maintain a dual-identity Git environment.


Phase 1: SSH Key Generation

Run these to create your unique digital IDs for each account.

# Generate Personal Key
ssh-keygen -t ed25519 -f ~/.ssh/id_self

# Generate Work Key
ssh-keygen -t ed25519 -f ~/.ssh/id_work

# View Public Key (to copy into GitHub Settings)
cat ~/.ssh/id_self.pub
cat ~/.ssh/id_work.pub

Phase 2: SSH Agent Management

Commands to start the memory manager and load your keys for the day.

# Start the SSH Agent
eval $(ssh-agent -s)

# Load your specific keys into the Agent
ssh-add ~/.ssh/id_self
ssh-add ~/.ssh/id_work

# Verify which keys are currently loaded
ssh-add -l

# Delete all keys from memory (to reset)
ssh-add -D

Phase 3: Connection Testing

Verify that your ~/.ssh/config aliases are working correctly.

# Test Personal Identity Handshake
ssh -T git@github.com-self

# Test Work Identity Handshake
ssh -T git@github.com-work

Phase 4: Git Identity & Audit

Commands to verify that your includeIf logic is swapping your name and email correctly.

# Check active email in current directory
git config user.email

# Audit exactly which file is providing the current settings
git config --list --show-origin

# See the 'Scope' of every setting (Global vs Local vs Work)
git config --list --show-scope

# Check current user name
git config user.name

Phase 5: Repository Operations

How to interact with projects using your new alias-based system.

# Clone a Work Repo using the alias
git clone git@github.com-work:OrgName/RepoName.git

# Clone a Personal Repo using the alias
git clone git@github.com-self:UserName/RepoName.git

# Update an existing project to use a specific alias
git remote set-url origin git@github.com-work:OrgName/RepoName.git

# Initialize a brand new folder to trigger includeIf logic
git init

Troubleshooting: Hidden Submodule Folder Lock

When running nested package scripts or initializing framework sub-folders (like app-mobile), a nested folder might contain an accidental hidden .git repository layer. This freezes parent indexing and blocks your files from staging.

# 1. Purge the nested workspace tracker out of the main index cache
git rm --cached [nested-folder-name]

# 2. Safely wipe out the hidden nested inner repository directory
rm -rf [nested-folder-name]/.git

# 3. Stage the entire unified tree together seamlessly
git add .

🚀 Onboarding Protocol for Collaborative Teams

To onboard collaborative developers into a dual-managed project environment efficiently without dependency runtime discrepancies, distribute this fast setup playbook:

# 1. Download the code map via your routed account identity alias
git clone git@github.com-self:Dheeraj23qw/wizblow.git

# 2. Install production-matched native project dependencies
npm install

# 3. Clear local Android build artifacts if build compilation errors occur
npm run clean

# 4. Initialize your local Android managed development client stream
npm run android

Master Command Reference Table

Command Usecase & Backend Function
ssh-keygen Creates the encryption files used for identity.
eval $(ssh-agent -s) Wakes up the background process that holds keys.
ssh-add [path] Puts a key into "Active Duty" for the current session.
ssh -T [alias] Asks GitHub: "Who do you think I am using this key?"
git config --list --show-origin The "Debugger" command. Points to the file path of every active setting.
git remote -v Shows if your project is using the alias or the default GitHub URL.
ls -al ~/.ssh Lists all files in your SSH folder to verify keys exist.

Reference this sheet whenever you switch machines or set up a new environment.

Built with ❤️ by a developer who survived the Git struggle.

About

A step-by-step guide to managing multiple Git and SSH identities on Windows. Automate switching between work and personal accounts using Git includeIf and SSH Host Aliases. No more 'Permission Denied' or wrong-email commits!"

Topics

Resources

License

Stars

2 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors