Let's walk through the full steps to generate a new personal_rsa key for your personal projects (if it doesn't already exist), set up the SSH configuration for both accounts, and smoothly switch between them.
First, let's check if you already have a personal SSH key, such as personal_rsa or id_rsa.
Run this command to list the SSH keys in your ~/.ssh/ directory:
ls ~/.ssh/You should see files named something like id_rsa, id_rsa.pub, personal_rsa, personal_rsa.pub, or fullstackmaster.
- If you see
personal_rsa, you already have a personal key and can skip to Step 3. - If you don't see
personal_rsa, continue to the next step to generate it.
If you don’t have an SSH key for your personal projects, you can generate one as follows:
-
Run the following command to generate a new SSH key pair named
personal_rsa:ssh-keygen -t rsa -b 4096 -C "[email protected]" -
When asked where to save the key, type the following path:
/Users/rupesh/.ssh/personal_rsa
-
If prompted for a passphrase, either choose a secure passphrase or press Enter to leave it empty.
After this, your personal_rsa private key and personal_rsa.pub public key will be created in the ~/.ssh/ directory.
-
Start the SSH agent if it's not already running:
eval "$(ssh-agent -s)"
-
Add your new
personal_rsakey to the SSH agent:ssh-add ~/.ssh/personal_rsa
Next, you need to add the newly generated public key (personal_rsa.pub) to your GitHub account.
-
Copy the public key to your clipboard:
cat ~/.ssh/personal_rsa.pub -
Click New SSH Key, give it a descriptive title (e.g., "Personal RSA"), and paste the copied key.
-
Click Add SSH Key.
Now that both your fullstackmaster and personal_rsa keys are ready, let’s configure SSH to switch between them automatically based on the repository.
-
Open or create the
~/.ssh/configfile usingvim:vim ~/.ssh/config -
Add the following configuration for both your
fullstackmaster1andpersonalGitHub accounts:# FullStackMaster GitHub account Host github-fullstackmaster HostName github.com User git IdentityFile ~/.ssh/fullstackmaster # Personal GitHub account Host github-personal HostName github.com User git IdentityFile ~/.ssh/personal_rsa
-
Save and exit
vimby pressingEsc, then type:wq, and press Enter.
You need to ensure the correct remote URLs are set for each repository.
Update the remote URL for your professional repositories to use the github-fullstackmaster host:
git remote set-url origin git@github-fullstackmaster:FullStackMaster1/repo-name.gitUpdate the remote URL for your personal repositories to use the github-personal host:
git remote set-url origin git@github-personal:rupeshtiwari/repo-name.gitTo test the setup, check the SSH connection for both accounts:
ssh -T git@github-fullstackmasterssh -T git@github-personalYou should see successful authentication messages for both accounts.
Now, when you push code, Git will automatically use the correct SSH key based on the repository's remote URL.
For professional repositories:
git push origin mainFor personal repositories:
git push origin mainBy setting up this configuration, you can easily switch between your professional and personal GitHub accounts without manually changing SSH keys.