Skip to content

Hassaan9744/git-multi-account

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 

Repository files navigation

Setting Up Multiple GitHub Accounts with Folder-Based Configuration (Linux, Windows, macOS)

This guide helps you set up and manage multiple GitHub accounts (e.g., personal and work) on a single machine using SSH keys and custom .gitconfig files.


πŸ” 1. Generate SSH Keys

Create separate SSH keys for each GitHub account:

Personal Account

ssh-keygen -t ed25519 -C "your_personal_email@example.com"
# Save as: ~/.ssh/id_personal_github

Work Account

ssh-keygen -t ed25519 -C "your_work_email@company.com"
# Save as: ~/.ssh/id_work_github

Add SSH Keys to the Agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_personal_github
ssh-add ~/.ssh/id_work_github

🌐 2. Add SSH Keys to GitHub

Step 1: Copy public key

cat ~/.ssh/id_personal_github.pub

Step 2: Add to GitHub

Go to GitHub β†’ Settings β†’ SSH and GPG keys β†’ New SSH key:

Title: Personal Key

Paste the copied key

Repeat for the work key (id_work_github.pub).

βš™οΈ 3. Create .gitconfig Files

Note: Make sure the username in your Git configuration matches your actual GitHub username. GitHub does not validate the username when using SSH keys, so if there’s a mismatch, it won’t show an errorβ€”but you might unintentionally push code under someone else’s account if that username exists.

Personal Folder (~/Code/personal/.gitconfig)

[user]
   email = your_personal_email@example.com
   name = personal_username
[github]
   user = "personal_username" # names should be in double quotes
[credentials]
   username = "personal_username"  # names should be in double quotes
[core]
   sshCommand = "ssh -i ~/.ssh/id_personal_github"

Work Folder (~/Code/work/.gitconfig)

[user]
   email = your_work_email@company.com
   name = work_username
[github]
   user = "work_username"  # names should be in double quotes
[credentials]
   username = "work_username"  # names should be in double quotes
[core]
   sshCommand = "ssh -i ~/.ssh/id_work_github"

πŸ—ƒοΈ 4. Global .gitconfig (in ~/.gitconfig)

# Default account - Personal
[includeIf "gitdir:~/Code/personal/"]
    path = ~/Code/personal/.gitconfig

# Work account - Organization
[includeIf "gitdir:~/Code/work/"]
    path = ~/Code/work/.gitconfig

[core]
    excludesfile = ~/.gitignore

βœ… 5. Verify the Active GitHub Account To confirm which GitHub account is being used:

Clone a repository using SSH from GitHub (either personal or work):

bash

git clone git@github.com:username/repo-name.git

Paste the cloned repo into both your personal and work folders (e.g., ~/Code/personal/ and ~/Code/work/).

In each folder:

Make a small change.

Commit the change:

git commit -am "Test commit"

Run the following to verify the identity:

git config user.name
git config user.email

Confirm that the name and email match the values defined in the .gitconfig file specific to that folder.

This ensures the correct GitHub account is being used with the corresponding SSH key and identity settings.

πŸ“ Folder Structure Example

~
β”œβ”€β”€ .ssh/
β”‚   β”œβ”€β”€ id_personal_github
β”‚   β”œβ”€β”€ id_personal_github.pub
β”‚   β”œβ”€β”€ id_work_github
β”‚   └── id_work_github.pub
β”œβ”€β”€ Code/
β”‚   β”œβ”€β”€ personal/
β”‚   β”‚   └── .gitconfig
β”‚   └── work/
β”‚       └── .gitconfig
└── .gitconfig

About

Setting Up Multiple GitHub Accounts with Folder-Based Configuration (Linux, Windows, macOS)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors