Manage multiple Git accounts
Background
When you've got several Git accounts and want to use them on one computer, logging in can get tricky. But fear not! You can sort it out by managing them all with SSH config.
Here's how to handle multiple Git accounts on your computer using SSH config. You can set it up for different Git services like GitHub, GitLab, or Bitbucket.
Let's see the steps in detail:
Generate SSH Key pairs
SSH keys are a secure and convenient way to authenticate with Git hosting services such as GitHub, GitLab, and Bitbucket.
To get started with SSH keys for your Git account, you'll first need to create a new pair of keys. You can do this using the ssh-keygen command. For instance, to make keys for your first account, you can use this command:
ssh-keygen -t rsa -b 4096 -C "email1@email.com"
Use this command to create a new set of keys for secure access to your Git accounts. Just specify your email, and it'll generate both a private and public key. The type of key (RSA) and its bit size (4096 bits) are set for you.
Typically, the keys are named id_rsa and id_rsa.pub. You'll need to add the public key to your Git hosting service to use it for managing your repositories.
Do the same for another git account you want to add and generate SSH key key pairs for it also.
ssh-keygen -t rsa -b 4096 -C "email2@email.com"
Add the public key to the respective git accounts
Go to github and Add the respective public key as new ssh key for both accounts. https://github.com/settings/keys
Use SSH config file to manage multiple Git accounts
create a config
file as
touch ~/.ssh/config
The .ssh folder should have key pair for both account :
Add the following content in your SSH config file to define aliases for each of these accounts:
Host github.com
HostName github.com
User rohanmrzan100
IdentityFile ~/.ssh/id_rsa
Host work.github.com
HostName github.com
User rohanmaharjan100
IdentityFile ~/.ssh/id_rsa_harness
Finally, Once you have defined these aliases in your SSH config file, you can use them to manage repositories using SSH URLs.
For example, to clone a repository using your personal account, you can use the following command:
git clone git@github.com:rohanmaharjan100/developer-hub.git
And clone a repository using your work account, you can use the following command:
git clone git@work.github.com:rohanmaharjan100/developer-hub.git
Easily manage different Git accounts on one computer without any login issues. Switch between accounts hassle-free when you clone repositories.
Thanks for checking out the guide on handling multiple Git accounts. Hope it helped! Any questions or feedback? Let me know.
Happy coding!