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.
So let's start talking about ssh , I'm not going to deep dive on them , I will just talk about the things that to related to this topic.
First 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 example if you want to access a server , A typical workflow is to go to website interface and type username and password , And imaging doing that each time ,
Using SSH give the ability to access the a server using generate key we call it public key.
you place that public key in the server and boom you can access the server using this
ssh [email protected]
But wait , Why we 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
I'm sorry for blowing you up with information but I think that you need to remember this from time to time.
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.
Using SSH , GitHub know which user has rights to access to a particular resource(a repo in our case) , And which user don't.
Since I'm suing my personnel account and I'm trying to access repo that I don't have access to it , and yeah I did find it by reading documentation 1.
I can do that by using ssh-agent-forwarding. I think I don't need to talk about ssh-agent , I will show on the demo how to do that.
To start first you need to check which account do you have or currently have authenticated.
ssh -T [email protected]
You're email GitHub will pop up in the terminal in this case.
To generate a key for the new GitHub account that you will use for work .
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "[email protected]"
If you notice this, I added _work to my name key , And 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]
take this content and put on GitHub
and then try
ssh -T [email protected]
Now the most of the work is done, We need a couple of things to setup this correctly .
Run this :
cat ~/.ssh/config
you will see something like this :
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yesAnd since we have a new one , We can use this :
vim ~/.ssh/config
and then add this :
# GitHub Work Account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_workand you can rename this :
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yesSave and quit.
Test authentication for work :
ssh -T git@github-work
and for personal
ssh -T git@github-personal
Now go the repo that you don't have access after running git clone:
git clone git@github-work:company/project.git
This is for personal :
git remote set-url origin git@github-personal:USERNAME/repo.git
And this is for work :
git remote set-url origin git@github-work:ORG_OR_USERNAME/repo.git
Then verify the remote :
git remote -v
now you can run :
git push git pull
very easily without any concerns , And hope this will help you guys.