DEV Community

nikhil mahato
nikhil mahato

Posted on

2

Push to multiple GitHub accounts!

Generate SSH Key

For each account, generate a separate SSH key:

ssh-keygen -t ed25519 -C "your-email-for-first-account" -f ~/.ssh/id_ed25519_first
ssh-keygen -t ed25519 -C "your-email-for-second-account" -f ~/.ssh/id_ed25519_second

Enter fullscreen mode Exit fullscreen mode
  • -t ed25519: Specifies the type of key (ed25519).
  • -C "your-email-for-first-account": Adds a comment for identification.
  • -f ~/.ssh/id_ed25519_first: Specifies the file name for the private key (~/.ssh/id_ed25519_first) and the public key will automatically be named ~/.ssh/id_ed25519_first.pub.

Add SSH Keys to the Agent

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

Enter fullscreen mode Exit fullscreen mode

List Existing SSH Keys

Run the following command to list your SSH keys:

ls ~/.ssh

Enter fullscreen mode Exit fullscreen mode

You should see files like id_ed25519, id_ed25519.pub, id_ed25519_first, id_ed25519_second, or similar. The .pub files are the public keys.

Add the Public Keys to GitHub

Copy each public key to GitHub:

cat ~/.ssh/id_ed25519_first.pub
cat ~/.ssh/id_ed25519_second.pub

Enter fullscreen mode Exit fullscreen mode

Add these keys to the respective GitHub accounts under Settings > SSH and GPG keys.

Configure ~/.ssh/config

Create or edit the ~/.ssh/config file to define host aliases:

nano ~/.ssh/config

Enter fullscreen mode Exit fullscreen mode

Add the following lines:

Host github-first
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_first

Host github-second
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_second

Enter fullscreen mode Exit fullscreen mode

Save the file.

Test SSH Keys

Use the following command to test your SSH keys with GitHub.
For the first account:

ssh -T git@github-first

Enter fullscreen mode Exit fullscreen mode

You should see a message like:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

Enter fullscreen mode Exit fullscreen mode

Check for the second account too!
If you encounter any issues, ensure:

  • The correct SSH keys are added to the agent.
  • The ~/.ssh/config file is correctly set up.

Clone Repositories

Use the alias for the desired account:

git clone git@github-first:username/repository.git
git clone git@github-second:username/repository.git

Enter fullscreen mode Exit fullscreen mode

Update an Existing Repository's Remote

If you're switching remotes:

git remote set-url origin git@github-first:username/repository.git
git remote set-url origin git@github-second:username/repository.git

Enter fullscreen mode Exit fullscreen mode

Push Code from VS Code(or any code editor)

1. Open the repository folder in VS Code.

2. Ensure the correct remote is set:

git remote -v

Enter fullscreen mode Exit fullscreen mode

3. Commit and push code:

git add .
git commit -m "Your commit message"
git push origin main

Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay