DEV Community

Nabin Bhatt
Nabin Bhatt

Posted on

Managing Multiple GitHub Accounts

Step 1: Create SSH keys

  1. Open PowerShell on your local machine.
  2. Run the following command to generate a new SSH key for your first GitHub account:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode
  1. Follow the prompts to specify a file location for the SSH key and set a passphrase (optional).

Step 2: Add SSH keys to the SSH agent

  1. Start the SSH agent by running the following command in PowerShell:
Start-Service ssh-agent
Enter fullscreen mode Exit fullscreen mode
  1. Add the private SSH key to the agent by running the following command, replacing <private_key_path> with the path to your private SSH key:
ssh-add <private_key_path>
Enter fullscreen mode Exit fullscreen mode

Step 3: Add the public key to your GitHub account

  1. Copy the contents of the public key file by running the following command in PowerShell:
Get-Content "<public_key_path>"
Enter fullscreen mode Exit fullscreen mode
  1. Go to your GitHub account settings in your web browser and navigate to "SSH and GPG keys".
  2. Click on "New SSH key" and paste the copied public key into the "Key" field.
  3. Give the SSH key a descriptive title and click on "Add SSH key" to add it to your GitHub account.

Step 4: Modify the SSH config file

  1. In PowerShell, run the following command to create a new SSH config file or open an existing one:
notepad $env:USERPROFILE\\.ssh\\config
Enter fullscreen mode Exit fullscreen mode
  1. In the text editor, add the following lines, replacing <alias> with an alias of your choice, and <github_username> with your first GitHub account's username:
Host <alias>
  HostName github.com
  User <github_username>
  IdentityFile <private_key_path>
Enter fullscreen mode Exit fullscreen mode
  1. Save and close the config file.

Step 5: Fork & clone repos

  1. In PowerShell, navigate to the directory where you want to clone a repository.
  2. Run the following command to clone a repository from your first GitHub account, replacing <alias> with the alias you set in the config file, and <repo_url> with the URL of the repository you want to clone:
git clone git@<alias>:<github_username>/<repo_url>.git
Enter fullscreen mode Exit fullscreen mode
  1. You may be prompted to enter the passphrase for your SSH key if you set one during the key generation process.

That's it! You can now work with multiple GitHub accounts using SSH keys and PowerShell. Remember to repeat these steps for each additional GitHub account you want to work with, using a different alias, SSH key, and GitHub username for each account.

Top comments (0)