Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

How to manage multiple GitHub accounts on a single machine with SSH keys

#1. Generating the SSH keys
# Before generating an SSH key, we can check to see if we have any existing SSH keys: ls -al ~/.ssh This will list out all existing public and private key pairs, if any.

$ ssh-keygen -t rsa
$ ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"
# We have two different keys created:

$ ~/.ssh/id_rsa
$ ~/.ssh/id_rsa_work_user1
# 2. Adding the new SSH key to the corresponding GitHub account
#  Copy the public key pbcopy < ~/.ssh/id_rsa.pub and then log in to your personal GitHub account:
#3 . Registering the new SSH Keys with the ssh-agent
#To use the keys, we have to register them with the ssh-agent on our machine. Ensure ssh-agent is running using the command eval "$(ssh-agent -s)".
# Add the keys to the ssh-agent like so:

$ ssh-add ~/.ssh/id_rsa
$ ssh-add ~/.ssh/id_rsa_work_user1

# 4. Creating the SSH config File

$ cd ~/.ssh/
$ touch config           # Creates the file if not exists
$ code config            # Opens the file in VS code, use any editor
# copy and paste content change it to your info
# Personal account, - the default config
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa
   
# Work account-1
Host github.com-work_user1    
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work_user1

#5. One active SSH key in the ssh-agent at a time
#This approach doesn’t require the SSH config rules. Rather we manually ensure that the ssh-agent has only the relevant key attached at the time of any Git operation.
#ssh-add -l will list all the SSH keys attached to the ssh-agent. Remove all of them and add the one key you are about to use.

#If it’s to a personal Git account that you are about to push:

$ ssh-add -D            #removes all ssh entries from the ssh-agent
$ ssh-add ~/.ssh/id_rsa  # Adds the relevant ssh key
 # or 
$ ssh-add -D
$ ssh-add ~/.ssh/id_rsa_work_user1

#To list the config name and email in the local Git directory, do git config user.name and git config user.email. If it’s not found, update accordingly.

git config user.name "User 1"   // Updates git config user name
git config user.email "user1@workMail.com"
# 6. While Cloning Repositories
# Repositories can be cloned using the clone command Git provides:

$ git clone git@github.com:personal_account_name/repo_name.git
$ git clone git@github.com-work_user1:work_user1/repo_name.git
#7. For Locally Existing Repositories
# If we have the repository already cloned:

$ git remote set-url origin git@github.com-worker_user1:worker_user1/repo_name.git
$ git remote add origin git@github.com-work_user1:work_user1/repo_name.git 
# the initial commit to the GitHub repository:

git add .
git commit -m "Initial commit"
git push -u origin master
# We are done!
Source by www.freecodecamp.org #
 
PREVIOUS NEXT
Tagged: #How #manage #multiple #GitHub #accounts #single #machine #SSH #keys
ADD COMMENT
Topic
Name
1+2 =