Table of contents
- What are SSH Keys?
- Setup SSH Key
- Step 1: Create an SSH key for the account you are working with.
- Step 2: Add SSH key to the SSH Agent.
- Step 3: Add the SSH public key to Github.
- Step 4: Create a config file in the .ssh folder and make host entries.
- Step 5: Test the configuration to confirm that it is correct.
- Step 6: Clone the repository needed and pull or push the changes made.
- Git Credential Method
- Summary
It is common for techies to have multiple GitHub accounts, one for work and the other for personal projects. Imagine a scenario where you are a new employee in a company and it is required to use your work account to work on company projects. To be able to clone any repository from the company GitHub account, you will need to be signed in to your local machine and clone using the right accounts. This would mean logging out of your personal account and into your work account. The whole process can be tiresome and is easily done using SSH(Secure Shell) Keys.
What are SSH Keys?
Firstly, SSH(Secure Shell) is a network communication protocol that enables communication and sharing of data between two computers or your computer and a remote server like Github. The communication is encrypted. The pair of public or private keys used to authenticate and establish the encrypted communication channel between the two machines are called SSH Keys. To learn more about SSH, click here.
Setup SSH Key
For this tutorial, I will use a test GitHub account with GitHub username tochi-clemas and email address exampleemail@gmail.com and vscode editor. Basic knowledge of git and github is required as well. The setup process can be done in these 5 steps:
Step 1: Create an SSH key for the account you are working with.
Step 2: Add the SSH key to the SSH Agent.
Step 3: Add the SSH public key to GitHub.
Step 4: Create a config file in the .ssh folder and make host entries.
Step 5: Test the configuration to confirm that it is correct.
Step 6: Clone the repository needed and push or pull changes.
Step 1: Create an SSH key for the account you are working with.
Open up the bash terminal and move into the .ssh folder
. If you do not already have a .ssh folder
, move to the step to generate a unique key. It automatically creates the folder for you in whatever directory you are in. It is preferred to create it in the root directory of your PC.
cd ~/.ssh
The next step is to generate a unique key for the account.
ssh-keygen -t rsa -C “accountemail@gmail.com” -f “github-username”
ssh-keygen: This command generates a new SSH key pair (public and private keys).
-t rsa: This option specifies the type of key to create.
-C: This option allows you to specify a comment for the key. Typically, this is used to add an email address to the key to help identify it.
-f: This option specifies the filename for the key files.
accountemail@gmail.com: The email address linked to the github account.
github-username: This is the base name for the generated key files.
Hit enter after that. It asks for a passphrase, you can leave it empty if you like and hit enter after.
Step 2: Add SSH key to the SSH Agent.
To do this, enter the command below to your terminal.
eval "$(ssh-agent-s)"
Then connect the generated key to the agent.
ssh-add ~/.ssh/<github-username>
The github-username
is the name used to store the key generated in the .ssh folder.
Step 3: Add the SSH public key to Github.
Enter the ls
command on bash to see the list of files in the .ssh folder. Open the file with the .pub
extension, copy the content and paste it on GitHub. The steps to do this are as follows;
Enter
code <filename>.pub
to open the file from your bash terminal or do it from the GUI.Copy the content of the file.
Navigate to Settings > SSH and GPG keys> generate new ssh key on GitHub and paste the file's content.
Step 4: Create a config file in the .ssh folder and make host entries.
Inside the .ssh folder, create a config
file and open the file with a text editor. Inside the file, add the content below to it.
Host github.com-<github-username>
HostName github.com
User git
IdentityFile ~/.ssh/github-username
Step 5: Test the configuration to confirm that it is correct.
To do this, run ssh -T
git@github.com
-<github username>
in your terminal. Once the test is successful, you will get a message that says: Hi tochi-clemas! You've successfully authenticated, but GitHub does not provide shell access*.*
Step 6: Clone the repository needed and pull or push the changes made.
The next and final step is to clone the repository to your local machine and make the necessary changes. To clone, copy the URL under SSH on the GitHub repository instead of the usual link under https. The command to clone will look like this: git clone
git@github.com
-<githubusername>:{ssh URL from Github}
.
However, to ensure that when changes are made to Github it is being made from the correct account, configure the right account on your local machine and then push. To do that set the email and username for the account on your local machine as such, git config
user.email
<account email>
and git config
user.name
<account username>
.
Git Credential Method
There are several methods to manage multiple accounts on your device but the SSH method is less tiring. Another involves deleting your GitHub credentials from the credential manager on your device and signing in to the required work account. The steps involved are:
Navigate to Control Panel >> User Accounts >> Credential Manager or, search for Credential Manager on your device.
Under Windows Credentials locate and remove GitHub credentials.
Configure your account to match the one you want to work with.
Push or pull the changes made.
Summary
Managing multiple GitHub accounts on the same PC can be a hassle, but using SSH keys makes the process more efficient and less tiresome. Following the outlined steps to set up SSH keys for each account, you can easily switch between your personal and work GitHub accounts without repeatedly logging in and out. The SSH method ensures that your development workflow remains smooth.
Alternatively, you can use the Git credential method, which involves managing your credentials through the Credential Manager on your device. While this method works, it requires you to delete and re-enter credentials each time you switch accounts, making it tiresome compared to the SSH method.
In conclusion, setting up SSH keys saves you time and effort while maintaining secure access to your repositories.