How to Manage Multiple Git Accounts in one computer?
I would like to take a moment and thank you for reading this newsletter. Every week I write a newsletter that adds value to software engineers.
If you find this helpful, do spread the word about this newsletter on your favorite social platform. It would mean a lot to me!
Why the need to manage multiple git accounts?
Imagine if you need to switch between two laptops for code contribution for multiple repositories within two different accounts having different roles and permissions. It is just super painful, right?
So here I am with the solution.
In this letter, I describe how you can manage multiple accounts within the same machine and contribute to repositories of different accounts.
How to manage?
Understand how to generate an SSH key.
The first and foremost thing you need to understand is how to generate ssh file. You can generate ssh file using the below command
ssh-keygen -t rsa -C "your_email@example.com"
You will be asked for the path where ssh-key must be generated. I prefer to keep them in the respective folder for simplicity. You can also keep all of them in
.ssh
folder.Once you are able to generate ssh-key you will see two files in the folder where you have provided the path. (ex: id_rsa and id_rsa.pub files will be generated in
.ssh
folder [if you use the default path]).Copy the contents inside the id_rsa.pub file and paste it to Git Hub (or any providers). That means you need to add it by creating new keys in GitHub or your provider.
Add ssh configuration file: The config file is added inside
.ssh
folder.## Example SSH config # Personal GitHub account Host personal.github.com HostName github.com IdentityFile ~/.ssh/id_personal AddKeysToAgent yes # Work GitHub account Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa AddKeysToAgent yes
clone the repository based on origin as mentioned in the config file
# Clone with personal credentials git clone git@personal.github.com:REPO.git # Clone with work credentials git clone git@github.com:REPO.git
set git config
user.name
anduser.email
locally for each repo using the commandgit config user.name YOUR_NAME git config user.email YOUR_EMAIL
Note: set remote origin if you already cloned a repo
// get origin url
git ls-remote --get-url origin
//set origin url
git remote set-url origin git@personal.github.com:REPO.git
Once you have done the above steps, you can now start contributing to multiple projects, the ssh config will be automatically switch between accounts based on your configuration.
Further references: Original Link | Archive Link