Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MinhHieu-Nguyen-dn/9301f14e173695ee334dccdbbd0909af to your computer and use it in GitHub Desktop.

Select an option

Save MinhHieu-Nguyen-dn/9301f14e173695ee334dccdbbd0909af to your computer and use it in GitHub Desktop.
[Windows Git] How To Work With Multiple Github Accounts on your Windows PC

(Windows version) How To Work With Multiple Github Accounts on a single Machine

Let suppose I have two github accounts, https://github.com/nmhieu-office and https://github.com/nmhieu-personal. Now i want to setup my Windows laptop to easily talk to both the github accounts.

NOTE: This logic can be extended to more than two accounts also. :) This can be used with either PowerShell or Command Prompt (cmd).

The setup can be done in 5 easy steps:

Steps:

  • Step 1 : Create SSH keys for all accounts
  • Step 2 : Add SSH keys to SSH Agent
  • Step 3 : Add SSH public key to the Github
  • Step 4 : Create a Config File and Make Host Entries
  • Step 5 : Cloning GitHub repositories using different accounts

Step 1

Create SSH keys for all accounts

First make sure your current directory is your .ssh folder. For example, your user profile named "hi", it should be: C:\Users\hi\.ssh>

     PS C:\Users\hi> cd ~/.ssh

Syntax for generating unique ssh key for ann account is:

     PS C:\Users\hi\.ssh> ssh-keygen -t rsa -C "your-email-address" -f "github-username"

here,

-C stands for comment to help identify your ssh key

-f stands for the file name where your ssh key get saved

Now generating SSH keys for my two accounts

     ssh-keygen -t rsa -C "[email protected]" -f "nmhieu-office"
     ssh-keygen -t rsa -C "[email protected]" -f "nmhieu-personal"

Notice here nmhieu-office and nmhieu-personal are the username of my github accounts corresponding to my_office_email@gmail.com and my_personal_email@gmail.com email ids respectively.

After entering the command the terminal will ask for passphrase, leave it empty and proceed. Result should look like this for creating each key:

image

Now after adding keys, in your .ssh folder, a public key and a private will get generated.

The public key will have an extention .pub and private key will be there without any extention both having same name which you have passed after -f option in the above command. (in my case nmhieu-office and nmhieu-personal)

image


Step 2

Start the ssh-agent first

     PS C:\Users\hi\.ssh> Start-Service ssh-agent
     PS C:\Users\hi\.ssh> Get-Service ssh-agent

     Status   Name               DisplayName
     ------   ----               -----------
     Running  ssh-agent          OpenSSH Authentication Agent

Then, Add SSH keys to SSH Agent

Now we have the keys but it cannot be used until we add them to the SSH Agent.

     PS C:\Users\hi\.ssh> ssh-add c:/Users/hi/.ssh/nmhieu-office
     Identity added: c:/Users/hi/.ssh/nmhieu-office (my_office_email@gmail.com)
     PS C:\Users\hi\.ssh> ssh-add c:/Users/hi/.ssh/nmhieu-personal
     Identity added: c:/Users/hi/.ssh/nmhieu-personal (my_personal_email@gmail.com)

You can read more about adding keys to SSH Agent here.


Step 3

Add SSH public key to the Github

For the next step we need to add our public key (that we have generated in our previous step) and add it to corresponding github accounts.

For doing this we need to:

1. Copy the public key directly to the clipboard (one by one)

We can copy the public key either by opening the .pub files in notepad (or any editor) and then copying the content of it, or copy directly to clipboard using this:

     PS C:\Users\hi\.ssh> Get-Content c:/Users/hi/.ssh/nmhieu-office.pub | clip
     PS C:\Users\hi\.ssh> Get-Content c:/Users/hi/.ssh/nmhieu-personal.pub | clip

2. Paste the public key on Github (one by one)

  • Sign in to Github Account
  • Goto Settings > SSH and GPG keys > New SSH Key
  • Paste your copied public key and give it a Title of your choice.

OR


Step 4

Create a Config File and Make Host Entries

The C:/Users/hi/.ssh/config file allows us specify many config options for SSH.

If config file not already exists then create one (make sure you are in C:/Users/hi/.ssh directory)

     PS C:\Users\hi\.ssh> New-Item -Path . -Name "config" -ItemType "file"

The commands below opens config in notepad.

     PS C:\Users\hi\.ssh> notepad config

Now we need to add these lines to the file, each block corresponding to each account we created earlier.

     #nmhieu-office account
     Host git-office
          HostName github.com
          User git
          IdentityFile C:\Users\hi\.ssh\nmhieu-office

     #nmhieu-personal account
     Host git-personal
          HostName github.com
          User git
          IdentityFile C:\Users\hi\.ssh\nmhieu-personal

So, your "host" for the nmhieu-office account is git-office; and for "nmhieu-personal" account is git-personal.


Step 5

Cloning GitHub repositories using different accounts

So we are done with our setups and now its time to see it in action. We will clone a repository using one of the account we have added.

Make a new project folder where you want to clone your repository and go to that directory from your terminal.

For Example: You are working in company project with owner MinhHieu-Nguyen-dn and need to clone the private repo machine_learning_from_scratch to your personal laptop. You have access to this repo with GitHub account nmhieu-office (company GitHub user - using company's email) but not nmhieu-personal (normal, default GitHub user on your local machine - using personal email). Now for cloning the repo use the below command:

    git clone git@{your host from Step 4}:{Repo's owner Git username}/{Repo's name}.git

    [e.g.] git clone git@git-office:MinhHieu-Nguyen-dn/machine_learning_from_scratch.git

Finally

From now on, to ensure that our commits and pushes from each repository on the system uses the correct GitHub user — we will have to configure user.email and user.name in every repository freshly cloned or existing before.

To do this use the following commands.

     git config user.email "[email protected]"
     git config user.name "nmhieu-office"
     
     git config user.email "[email protected]"
     git config user.name "nmhieu-personal"

Pick the correct pair for your repository accordingly.

To push or pull to the correct account we need to add the remote origin to the project

     git remote add origin git@{host}:{Git username}
     
     git remote add origin git@git-personal:nmhieu-personal
     
     git remote add origin git@git-office:nmhieu-office

Now you can use:

     git push
     
     git pull

P.S:
If this gist has been helpful to you, kindly consider leaving a star.
Hope it helps! Contact me at LinkedIn.
Original Author (Mac/Linux version): LinkedIn.

@y2bd
Copy link

y2bd commented Feb 21, 2025

Thanks for writing this up!

@MinhHieu-Nguyen-dn
Copy link
Author

Thanks for writing this up!

Hope it helps! I did struggle myself as well :)

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