Manage Multiple Github Accounts with SSH keys

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today let's take a look at how to manage multiple GitHub accounts using SSH Keys first thing first I'm starting fresh So currently I don't have any keys on my.ssh folder to add a SSH key I'm going to log into my tech car account go to settings and select SSH I can follow the instructions here to generate a key I'm going to copy the command and paste it on the terminal without the comment since it's only optional now the first prompt asks me for the file name for this new key rather than using the default I'm going to explicitly name it as iD Tech Cara indicating that this key is for my tech car account for the rest of the prompts I'm just going to use the default options once the key is generated I'll need to add this key to the SSH agent first type in SSH add Dash L to list the keys added to the agent I have none at the moment then I can add the Takara private key file after which I can verify the key has been added okay great I can now copy the public key not the private key but the public key that ends with DOT Pub extension and paste it into GitHub SSH setting all right now the key has been added let's verify I can access this account I'm going to select the first repo here and copy the SSH address not the https address when I do git clone oh I already have the folder so let me first erase this folder again git clone add the fingerprint and I can successfully clone the repo awesome that's the first step now let's add a key to a different GitHub account let me log out and sign in with a different account to say a work account let me first try to add the same key to this account again settings SSH and new key pass the previous key as you can see here GitHub complains that the key is already in use meaning that I cannot add the same key to two different accounts all right so that essentially means that I will need to create a new key and add that to this account so I'm going to do the same thing as before this time I'm going to name the key as Warcraft since this is for my workara account I should have four Keys a public key a private key for each account don't forget to add the new key into the agent with SSH add command again copy the new public key and paste it into the GitHub SSS settings and this time GitHub doesn't complain so let's test a repo from this account again copy the SSH address and clone well here is our problem I can't access the work account even if I added the SSH key to the GitHub accounts and to the SSH agent this is because I have multiple keys but the git clone only tries the first one which is the key for techhara account what I could do is delete the tech hierarchy from the agent with Dash D option after which I'm left with only one key which is workhara account so if I try to clone a report from the workhara account now I can do it successfully in fact every time I want to work with a different account I need to repeat this deleting the existing account key and adding the new account key which is kind of annoying if we have to frequently switch back and forth between accounts for example if I go back to the repo from the first techhara account create a new branch and push to GitHub it refused to do it because my SSH agent does not have the key again I can delete the second key at the first key and I should be able to do this but I'm getting an error this is because the second key hasn't been deleted yet so let me delete it and try it again yes so now I can push the change to the first account again so the solution to this hassle is to save this into the SSH config here is a good gist I found on this topic which I'm going to share the link down below if you scroll down here you can find a section on SSH config file what I'm going to do is to copy the button to configs which is for git live but should work the same for GitHub and paste it into my.ssh folder the host here is the Alias that I'll refer to so it can be anything but I'm just gonna write it as techhara.github.com the host name here is the actual address for the Alias so that's going to be GitHub in my case and I can change the identity file to point to my tech card private key oh and let me also change the comment as well I can now copy the first into the second and simply replace Takara into our Kara in host and identity key sections and save and exit with that I'm going to add back the workara key to the agent so I now should have both keys now if I make a change in push I can do it on the first account it seems as if we are done but we are not actually done to demonstrate this if I go back to the repo from the second account make a change in push it won't do it even if I have both keys added to the agent that's because I haven't changed the repo address to reflect the host I specified in the SSH config to fix that simply open up the config file in the dot git directory and changed github.com to the host which in this case will be workara.github.com now I can push the change alright similarly I can do the same thing with the first account techhara account so let me go back to the first account and again edit GitHub config to read takara.github.com and again I can make the change and I can successfully push so once I set up the SSH config and GitHub config I can now go back and forth between the accounts seamlessly alright that's all for today's video and hope you found it useful
Info
Channel: TechHara
Views: 20,721
Rating: undefined out of 5
Keywords: github, ssh, gitlab, work, personal, account
Id: jGwD3e1BZ5Y
Channel Id: undefined
Length: 5min 53sec (353 seconds)
Published: Mon Dec 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.