CodingTricks LogoCodingTricks
HomePostsTipsCopy/PasteLinksContact UsAbout Us
2024 - 2025 CodingTricks.co | All rights reserved
Privacy PolicyTerms of Service
How to Manage Multiple GitHub Accounts on the Same Computer

How to Manage Multiple GitHub Accounts on the Same Computer

Posted by

kamlesh paul

on

Dec 10, 2024

| 4 min read

Last updated on : Dec 10, 2024

Server
159 views

#Table of contents

  • Overview:
  • How it works:
  • Steps to Manage Multiple GitHub Accounts:
    • Generate SSH Keys for Each GitHub Account
    • Add SSH Keys to the SSH Agent
    • Add SSH Public Keys to GitHub
    • Configure the SSH Config File with Aliases
    • Clone and Manage Repositories Using the Correct Account
    • Adding a Remote to an Existing Repository
  • Conclusion

#Overview:

Managing multiple GitHub accounts on the same computer can be done by creating separate SSH keys for each account and setting up a configuration file to handle them. This setup prevents conflicts between accounts and allows you to easily clone repositories and push changes without confusion.

#How it works:

  1. You’ll generate a separate SSH key for each GitHub account.
  2. Configure the ~/.ssh/config file to map each key to an alias for GitHub, making it clear which key corresponds to which account.
  3. You can then clone, push, and pull from repositories using the correct account by referencing the custom aliases.

#Steps to Manage Multiple GitHub Accounts:

#Generate SSH Keys for Each GitHub Account

  • To get started, create an SSH key for each account. First, go to your .ssh directory:
cd ~/.ssh

Now generate the SSH keys for your GitHub accounts. In this example, we’ll create SSH keys for two accounts—one for a client account and one for a side project:

ssh-keygen -t rsa -C "client_email@example.com" -f "github-client"
ssh-keygen -t rsa -C "side_project_email@example.com" -f "github-sideproject"

This command creates a private key and a corresponding public key.

mulitple-ssh-key.webp

#Add SSH Keys to the SSH Agent

After generating the keys, add them to your SSH agent:

ssh-add ~/.ssh/github-client
ssh-add ~/.ssh/github-sideproject

This step ensures that the keys are available for authentication.

#Add SSH Public Keys to GitHub

Next, you’ll need to add the public keys to your GitHub accounts. To do this, first copy the public keys:

pbcopy < ~/.ssh/github-client.pub
 
pbcopy < ~/.ssh/github-sideproject.pub

Then, log in to each GitHub account and go to:

  • Settings > SSH and GPG keys > New SSH Key

Paste the corresponding public key and give it a descriptive title (e.g., “Client Account” or “Side Project”).

#Configure the SSH Config File with Aliases

To avoid confusion between accounts, you’ll need to create aliases in your SSH config file. If you don’t already have a config file, create one:

touch ~/.ssh/config 
open ~/.ssh/config

Now, add the following lines to the config file to set up aliases for each account:

# Client account
Host github.com-client
     HostName github.com
     User git
     IdentityFile ~/.ssh/github-client
 
# Side project account
Host github.com-sideproject
     HostName github.com
     User git
     IdentityFile ~/.ssh/github-sideproject

By creating aliases like github.com-client and github.com-sideproject, you can easily differentiate between accounts when interacting with GitHub.

#Clone and Manage Repositories Using the Correct Account

When you want to clone a repository, use the alias you created in the SSH config. For example, to clone a repo using your client account:

git clone git@github.com-client:username/client-repo.git

And to clone a repository for your side project:

git clone git@github.com-sideproject:username/sideproject-repo.git

This ensures that the correct SSH key is used for each account.

#Adding a Remote to an Existing Repository

If you already have a Git repository set up locally and need to add a remote for a specific GitHub account, you can add the remote manually using the correct alias from your SSH config. Here’s how to do it:

  1. Navigate to your project’s directory:
cd path/to/your/repo
  1. Add the correct remote for the account you want to associate with this repository. For example, to add a remote for your client account, use:
git remote add origin git@github.com-client:username/client-repo.git

For your side project account, use:

git remote add origin git@github.com-sideproject:username/sideproject-repo.git
  1. Verify the remote has been added:
git remote -v

You should see the remote URL associated with the correct account.

Now, whenever you push or pull changes from this repository, Git will use the correct SSH key based on the alias you provided. If needed, you can also switch between remotes by using git remote set-url to update the remote URL.

For example, if you need to change the remote to another account later:

git remote set-url origin git@github.com-sideproject:username/new-repo.git

#Conclusion

Setting up multiple GitHub accounts on the same computer is easy by following these steps. Using SSH keys and configuring the ~/.ssh/config file allows you to manage client work, side projects, and personal repositories seamlessly without any confusion. This method can be scaled to handle as many GitHub accounts as needed.

Related Posts

  • How to Customize FrankenPHPHow to Customize FrankenPHP
  • How to Protect Your Laravel from Spam IPs Using the Laravel Abuse IP PackageHow to Protect Your Laravel from Spam IPs Using the Laravel Abuse IP Package
  • How to setup Laravel Queues on cPanel: A Step-by-Step GuideHow to setup Laravel Queues on cPanel: A Step-by-Step Guide
  • How to Protect Your Linux Server from Brute Force SSH AttacksHow to Protect Your Linux Server from Brute Force SSH Attacks
  • Next.js Deployment Script for Zero Downtime on VPS with PM2Next.js Deployment Script for Zero Downtime on VPS with PM2

Tags

Api(1)Authentication(5)Backup (1)Copy Paste(12)Email(2)Express(1)Firebase(1)Github Action(2)News(8)Push Notification(1)Queue(2)Server(11)Server Action(3)Testing(1)Tips(17)Websocket(1)

Popular Posts

  • TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!  TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!
  • How to use WebSocket in NextJS App router with Socket.IOHow to use WebSocket in NextJS App router with Socket.IO
  • How to Set Up Queue Jobs in NextJS Using BullMQHow to Set Up Queue Jobs in NextJS Using BullMQ
  • Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)
  • How to Set Up NextJS cron jobs without VercelHow to Set Up NextJS cron jobs without Vercel
  • Mastering Laravel Streamed Responses: Boost Performance with Fast Data DeliveryMastering Laravel Streamed Responses: Boost Performance with Fast Data Delivery
  • Tinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHPTinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHP
  • Nextjs 14 roles and permissions (RBAC) : Step-by-Step GuideNextjs 14 roles and permissions (RBAC) : Step-by-Step Guide

Get updates directly to your inbox.

Join 500+ developers getting updates on Laravel & Next.js tips. No spam,
unsubscribe anytime.


Share this article:

159 views