Managing multiple GitHub accounts on Windows


During this blog, I have used the following sample details to configure 2 different GitHub accounts on Windows machine.


Step 1: Download and install “Git for Windows” from here.

Step 2: Generate RSA Keypair for the work account “work@example.com”. To diffrentiate the name of the file, save it as “id_rsa_work_account”

ssh-keygen -t rsa -C "work@example.com"

Step 3: Generate RSA Keypair for the personal account “personal@gmail.com”. To diffrentiate the name of the file, save it as “id_rsa_personal_account”

ssh-keygen -t rsa -C "personal@gmail.com"

Step 4: The private and public key are generated for the personal and work account.

Step 5: Copy the content of public key for work account.

more id_rsa_work_account.pub

Step 6: Login to the work GitHub account and navigate to “Settings”.

Step 7: Navigate to “SSH and GPG Keys” and click on “New SSH Key” button.

Step 8: Provide the “Title” and paste the content of public key which was copied in Step 5.

Step 9: As shown below the SSH public key is added to work GitHub account.

Step 10: Copy the content of public key for personal account.

more id_rsa_personal_account.pub

Step 11: Add the public key content to personal GitHub account.

Step 12: Navigate to “ssh_config” file and open it to modify

%SystemDrive%\Program Files\Git\etc\ssh\ssh_config

Step 13: Add the mapping of ssh public key in ssh_config as shown below

Host work.github.com
    HostName github.com
    User git
    IdentityFile C:\Users\Sanjay\.ssh\id_rsa_work_account

Host personal.github.com
    HostName github.com
    User git
    IdentityFile C:\Users\Sanjay\.ssh\id_rsa_personal_account

Step 14: Copy the ssh command for the repository which you want to clone to local system.

Step 15: Replace the hostname configured in Step 13 in the SSH command used to clone repository.

git clone git@$HOSTNAME$:$USERNAME$/$REPOSITORY$.git

OR

git clone ssh://git@$HOSTNAME$/$USERNAME$/$REPOSITORY.git

Step 16: Navigate to cloned repository and configure the username and email address which was used to commit and push the changes to git repository.

git config user.name "$USERNAME$"
git config user.email "$EMAIL$"

Step 17: Use the either of the following command to push the changes to GitHub repository.

git add .
git commit -m "$COMMIT_MESSAGE$"
git push git@$HOSTNAME$:$USERNAME$/$REPOSITORY$.git

OR

git add .
git commit -m "$COMMIT_MESSAGE$"
git push ssh://git@$HOSTNAME$/$USERNAME$/$REPOSITORY.git

OR

git add .
git commit -m "$COMMIT_MESSAGE$"
git push

Note: The command “git add .” will add all the updated files to the commit, So if you want to add specific file to commit then you may use the following command.

git add $file_name$
Avatar
Sanjay Gondaliya
Technical Director

My research interests include automation in pentration testing.