How to setup multiple GitHub accounts on Ubuntu
Git is a crucial tool for version control in software development, enabling you to manage code changes across personal projects, open-source contributions, and collaborations within different organizations, making it essential to handle multiple Git user accounts effectively.
Connecting your local environment to your GitHub accounts
The concept is straightforward each GitHub account should have its own associated SSH key, which consists of a pair of public and private keys. The private key is stored locally on your machine, while the public key is added to your GitHub account. Therefore, the number of SSH keys you have corresponds to the number of GitHub accounts you use. For instance, with two GitHub accounts — a personal and a professional one — you’ll need two SSH keys (two private keys and two public keys).
Check existing SSH keys on your local machine
ls -al ~/.ssh
By default,
- id_rsa
- id_rsa.pub
Create new SHH keys
ssh-keygen
Output
Generating public/private rsa key pair.
Enter file in which to save the key (/your_home/.ssh/id_rsa):offic
for references
Check Configuration File
Inside the .ssh
folder, create a file named config
.
# Company Github Account
Host companyGit
HostName github.com
IdentityFile ~/.ssh/office-key
User git
IdentitiesOnly yes
AddKeysToAgent yes
# Personal Github Account
Host personalGit
HostName github.com
IdentityFile ~/.ssh/id_rsa
User git
IdentitiesOnly yes
AddKeysToAgent yes
Creating a New Repository
# For personal account
git remote add origin git@personalGit:personal-account-username/new-repo.git
# For company account
git remote add origin git@companyGit:company-account-username/new-repo.git
Cloning an Existing Repository
# For personal account
git clone git@personalGit:personal-account-username/existing-repo.git
# For company account
git clone git@companyGit:company-account-username/existing-repo.git