I have been using git for months now and it’s been very useful in version controlling my projects. I created a GitHub Account then used my cmd to communicate to the account. Working with Git and GitHub has been very smooth not until I created another GitHub Account for my freelance projects. It says that Git is not permitting me to make changes to this new account because I do not have permission to do so.
This is where I get into digging for solutions and found Truth Seekers YouTube Video which tackles exactly what I needed. I decided I want to write about it just to share another perspective on how I managed to configure two GitHub Accounts in my machine. For context, I have a personal GitHub account which is logged in to my Chrome Browser and another secondary account logged in to my Opera Browser I use in freelancing.
Goals of this article:
A. Key Generation.
SSH is an encryption that, when used with Git and GitHub, allows us to transfer data securely from a local machine to a remote repository. SSH is amazing; it is an abbreviation for Secure Shell. We need a key pair for this to work: a public key and a private key. The public key will be added to a GitHub account, and then the private key will be stored on our local machine. Once a connection is made, the machine will check if the private key and the public key match. If they do, we can proceed with the data transfer.
To do this, first cd to C:\\Users\Dexter.
(Note: I will use my computer for these examples, you just need to change your path according to your machine and you should be good).
cd C:\\Users\Dexter
ssh-keygen -t rsa -C “testEmail1@gmail.com“
Running these two commands will create a private-public key pair but first, you need to specify which folder these files will be saved. It’s going to suggest
C:\\Users\Dexter/.ssh/id_rsa
but be cautious because it will override an existing id_rsa file. This time though, we will let this override the file. Next, we will set a passphrase for increased security. This is not mandatory you can set it as blank for no passphrase. What’s the difference? Every time you perform git push it will prompt you to enter your passphrase, it will be annoying in the long run but it’s still your call. For me, I want extra protection so I will set mine to
************************
After this, your keys as testEmail1@gmail.com is now saved in the .ssh folder.
B. Add an SSH Key on testEmail1@gmail.com GitHub Account.
GitHub > Settings > SSH and GPG Keys > new SSH Key
Title
*name this the machine you are using to access github*
Key
*this going to be the text you’re going to copy from id_rsa.pub*
C. Add the Key to SSH Agent
You need to run this command to activate ssh-agent. Activating ssh-agent will let you run the ssh-add command.
start-ssh-agent
You can now add the private key by running:
ssh-add C:\\Users\Dexter/.ssh/id_rsa
A message should be displayed telling you that your identity as testEmail1@gmail.com has been added.
D. Testing First Account.
git init
git config –global user.name “yourUsername”
git config –global user.email yourEmail
git config –global init.defaultBranch main
git add –all
git commit -m “Initial Commit.”
git remote add origin githubSshUrl
git push -u origin main
*enter passphrase*
If this does not work, jump to step G. If this works, you’re now ready for the second account. We will just repeat most of the steps earlier.
E. Generate Key.
ssh-keygen -t rsa -C “secondTest@gmail.com“
Then, create teh other id_rsa file but instead of id_rsa, we will name it id_rsa_secondary. I am adding a passphrase for this account as well.
C:\\Users\Dexter/.ssh/id_rsa_secondary
**********
After this, your keys as secondTest@gmail.com is now saved in the .ssh folder.
F. Add a New SSH Key in secondTest@gmail.com GitHub Account.
GitHub > Settings > SSH and GPG Keys > new SSH Key
Title
*name this the machine you are using to access github*
Key
*this going to be the text you’re going to copy from id_rsa_secondary.pub*
G. Add Key to ssh-agent.
If the prompt says: cannot connect to agent when you run the ssh-add command, follow these steps. It’s from this stack overflow post.
You should now be able to use ssh-agent. If not, try restarting the PC and then reopen cmd. Now, run the command:
ssh-add C:\\Users\Dexter/.ssh/id_rsa_secondary
The identity as secondTest@gmail.com should now be added.
Now that we have two accounts registered, the computer will be confused when using GitHub, so we need to create a configuration file.
H. Create a Configuration File.
The file name is “config” without any extension. It must be created inside the .ssh folder. This is the format:
# MAIN ACCOUNT
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# SECONDARY ACCOUNT
Host github-secondary
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_secondary
By now, your directory should look like this:
Now, the computer will be able to tell apart which has access to which account. You should now be able to push and pull repositories on both accounts but with some minor differences.
I. Test the Second GitHub Account.
git init
git config –global user.name “yourUsername”
git config –global user.email yourEmail
git config –global init.defaultBranch main
git add –all
git commit -m “Initial Commit.”
git remote add origin githubSshUrl
git push -u origin main
*enter passphrase*
Congratulations! Evertything should work fine now.
In summary, using SSH to set up multiple GitHub accounts on Windows 11 allows for safe and smooth sharing of data between your computer and GitHub. Learning how to generate key pairs and use SSH keys helps you manage different accounts more easily. By following the steps outlined, you can handle multiple accounts better, work more smoothly with others, and feel more confident about controlling different versions of your projects.