How to add Multiple Git accounts to your machine

Chandima Jayamina
2 min readApr 18, 2024
  1. Check for Existing SSH Keys:

Before generating a new SSH key, you may want to check if you already have an SSH key pair on your system. By default, SSH keys are usually stored in the .ssh directory in your home directory (~/.ssh). You can check for existing SSH keys by running the following command in your terminal or command prompt:

ls -al ~/.ssh

Look for files named id_rsa (private key) and id_rsa.pub (public key). If these files exist, you can skip the next step and use your existing SSH key.

2. Generate a New SSH Key:

If you don’t have an existing SSH key pair, you can generate a new one by running the following command in your terminal or command prompt:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Replace "your_email@example.com" with the email address associated with your GitHub account. Press Enter to accept the default file location (~/.ssh/id_rsa) and to not set a passphrase if you don't want to use one.

3. Add SSH Key to SSH Agent (Optional):

If you’re using an SSH agent to manage your SSH keys, you can add your SSH key to the agent by running the following command:

eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa

4. Copy Public Key to GitHub:

Next, you need to copy the contents of your public key (~/.ssh/id_rsa.pub). You can do this by running the following command in your terminal or command prompt:

cat ~/.ssh/id_rsa.pub

Copy the output of this command, which is your public key.

5. Add SSH Key to GitHub: Log in to your GitHub account and go to “Settings” > “SSH and GPG keys” > “New SSH key”. Paste your public key into the “Key” field and give it a title (e.g., “My SSH Key”). Click “Add SSH key” to save it.

6. Test SSH Connection: Finally, you can test your SSH connection to GitHub by running the following command in your terminal or command prompt:

ssh -T git@github.com

You should see a message confirming that you’ve successfully authenticated with GitHub.

Once you’ve completed these steps, your SSH key should be set up and ready to use with your GitHub account. You can now use SSH for authentication when interacting with GitHub repositories.

--

--