Why do I need to use multiple GitHub accounts? I'm using only one for everything. Well if you did ask you're self this question, you let me think that you're not employed or you're employed in non professional company. sorry but it's the truth. or you can stop reading here. This is only for professionals people or for people that want to be professional in their career.
Let's talk about ssh, I will just talk about the things that related to this topic. Ssh stands for secure shell or secure socket shell, is a network protocol that gives users -- particularly systems administrators -- a secure way to access a computer over an unsecured network.
For instance, if you want to access a server, a typical workflow is to go to a website interface and type username and password, and imaging doing that each time. Using SSH allows you to access the server using a generated key, this key know as public key. Simply, put that public key in the server and you can access the server using this :
ssh [email protected]
You may ask you're self, why we are talking about this, well we can use use ssh for variety of use cases:
- Server management like install updates, change configuration, monitor the server ,etc.
- Transfer file from local to remote server(SCP/SFTP) .
- Git Interaction.
Another definition from GitHub documentation :
Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username and personal access token at each visit. You can also use an SSH key to sign commits.
This overview is detailed, but it provides the necessary context for what follows.
Adopting SSH, GitHub know which user has rights to access to a particular resource(a repository in our case), and which user don't. Since I'm using my personnel account and I'm trying to access a repository that I don't have access to it, I need to find a way to access the repository without deleting my Github account and connect to the work Github account, I did find a way. This can be achieved by using ssh-agent-forwarding, I think I don't need to talk about ssh-agent, I will show a demo for how to do that.
Initially, you need to check which account you are currently authenticated to.
ssh -T [email protected]
you're email GitHub will pop up in the terminal in this case. and to generate a key for the new GitHub account .
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "[email protected]"
If you notice, I added _work to my name key.
This will create:
Private key : ~/.ssh/id_ed25519_work
Public key : ~/.ssh/id_ed25519_work.pub
Run this: cat ~/.ssh/id_ed25519_work.pub
You will see something like this :
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... [email protected]
Look in Github for SSH and GPG keys section and put content prompt and then try:
ssh -T [email protected]
Now the most of the work is done, to setup this correctly we need a couple of things to do .
Run this: cat ~/.ssh/config
You will see something like this :
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Since we have a new one, we can use this: vim ~/.ssh/config
Add this:
# GitHub Work Account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
You can rename Host github.com by something like this :
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Save and quit.
Test authentication for work and personal account:
ssh -T git@github-workssh -T git@github-personal
Finally, go the repository that you don't have access after running git clone:
git clone git@github-work:company/project.git
For personal account: git remote set-url origin git@github-personal:USERNAME/repo.git
For work account: git remote set-url origin git@github-work:ORG_OR_USERNAME/repo.git
Verify the remote: git remote -v
You can run now :
git push
git pull