Working with multiple SSHs
I needed to learn how to work with multiple SSHs at the same laptop to work in different GitHub accounts, here I'm documenting and sharing my experience if me or anyone needs to review how do this.
Tags
Working with multiple SSHs
I needed to learn how to work with multiple SSHs at the same laptop to work in different GitHub accounts, here I'm documenting and sharing my experience if me or anyone needs to review how to do this.
Introduction
I have two GitHub accounts, one for personal projects and another for work projects. I needed to work with both accounts at the same time, so I needed to learn how to work with multiple SSHs at the same laptop.
Solution
When we clone a repository from GitHub, we can use the SSH protocol to clone the repository according to a certain host.
For example, when we click on "Code" to clone the repo, we can see that the default host is "github.com", we can configure in our local machine to use a certain ssh according to this host.
Step 1: Creating the config file
After generating the SSH keys, and added them to the GitHub account, we can create our config file in the ~/.ssh
directory.
touch ~/.ssh/config
Step 2: Editing the config file
We can edit the config file to add the configuration for each host.
Host personal
HostName github.com-personal
User git
IdentityFile ~/.ssh/id_rsa_personal
Host work
HostName github.com-work
User git
IdentityFile ~/.ssh/id_rsa_work
Step 3: Cloning the repository or changing the remote URL
- Cloning the repository using the host we configured.
After adding the configuration to the ~/.ssh/config
file, we can clone the repository using the host we configured.
# Cloning the repository using the personal host
git clone git@github.com-personal:vertocode/vuetage.git
# Cloning the repository using the work host
git clone git@github.com-work:vertocode/vuetage.git
- Adding the remote URL
If we already have the repository cloned, we can add the remote URL to use the host we configured.
# Adding the remote URL to use the personal host
git remote add personal git@github.com-personal:vertocode/vuetage.git
# Adding the remote URL to use the work host
git remote add work git@github.com-work:vertocode/vuetage.git
And, if you need to remove the old remote URL, you can use the following command(You can use it before to add the remote and use the same name to can push using the common "origin" name to push to the repository, but you need to remove the old remote URL first):
git remote remove origin
Note
You can have two accounts configured, I mean, 2 SSHs configured, so you can commit and push to the repository using different accounts, even it's the same repository, but these accounts need to have permission.
Conclusion
I learned how to work with multiple SSHs at the same laptop to work in different GitHub accounts, and I documented and shared my experience if me or anyone needs to review how to do this.
I hope this article can help you to work with multiple SSHs at the same laptop to work in different GitHub accounts.
If you have any questions, feel free to ask me on Contact.
Thank you for reading!