How to Manage Multiple GitHub Accounts on the Same Computer
Posted By
kamlesh paulon
Dec 10, 2024Table of contents
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:
- You’ll generate a separate SSH key for each GitHub account.
- Configure the
~/.ssh/config
file to map each key to an alias for GitHub, making it clear which key corresponds to which account. - 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:
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
:
This command creates a private key and a corresponding public key.
Add SSH Keys to the SSH Agent
After generating the keys, add them to your SSH agent:
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:
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:
Now, add the following lines to the config file to set up aliases for each account:
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:
And to clone a repository for your side project:
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:
- Navigate to your project’s directory:
- 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:
For your side project account
, use:
- Verify the remote has been added:
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:
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.