Push code to GitHub with an SSH key

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the things that are super overlooked are ssh keys how to push and pull to a git repository using ssh remember when i first started i was just like it drives you crazy you have to constantly search around but typically in this video i want to show you how to actually connect to your private repos using ssh keys you could do with github or bitbucket in this example i'm going to be using github guys stay tuned because i'm going to show you how to do it in two ways one we're going to create one from the clone something that we have remotely push it on our local computer uh pull it on our local computer and then push changes and the second way stay tuned i'll show you the second step which is if we start with making a repo locally on our computer then we want to push it to our repo so then we have to actually create a remote one connect the repos and then push so it's two different ways so stay tuned this video i'm going to show you how to do both guys if you like front end web development full stack next gs react css hit that like button and come join me on this journey and let's just go right now i have um a basic you know this is a public repo this will work if it's private or public so i just have like a sample repo of repo that i'm going to be using and you know right here if you go to code you could see the ssh so here's the https which we're not going to be bothering with we're going to be using ssh so how do we use ssh basically how it works is you have a private key and a public key created on your computer and the private key is the real key but the public key is like the doorway so you basically create this public and private key on your computer and then on github or on wherever you're going to put it you give them the public key so then when you when you connect it basically listens for the public key on your computer and says do you have the private key and when you do it says okay you got in and now you can come in so we need to do two things we need to create a key on our on our private computer and then on the remote area we have to actually create add the key there so they can all talk to each other so let me show you how that works so the first thing i'm gonna do is i'm gonna just clone this because this is a public repository there shouldn't be a problem with that so i will actually clone clone this um and show you how that works now if it's private it won't let you clone it unless you have the key but let's just do this part because we're going to do that a little later so cd development i'm going to go right in i'm going to put this in my development folder so git clone and you literally just copy and paste the ssh i will paste it right here um oops i actually already did this so i will actually just delete what i have and restart all right so let's try again git clone do it so there it goes it's done it it download everything this is a public repo anyone could have access to this so there's no problem but no one can push to this except me um actually let's go ahead let's try to push something let's say get let's let's go into that route all right so we're in there let's go get push and look what it says get denied um here because you know you can't read from that remote repository make sure you have the you know the access key so now what we're going to do is we're going to go ahead and create an access key so if you just search generate ssh key you'll see it here is basically the default you want to go into this area which is basically the default i'm on a mac user's omar.ssh and if you don't have that you can go to create that and it's a it's a hidden folder that stores all the keys so you want to go into that folder so then cd.ssh there we are so we're in the folder okay great now we're going to go paste that in ssh key now i don't want to call it idrsa because you probably already have one and if you keep this name it's gonna write over the name so you don't wanna write over a key because you're probably using that key for something else so i'm gonna call this one github uiw and i'm gonna call it you know mac mini for this you can name it whatever you want um and you can share these keys on multiple computers so that's what's cool if you copy and paste it you can use the same one you can enter passphrase i don't want to bother with that because if you don't have the private key i mean you probably you know you're it's fine um but if someone were to steal the key off your computer they may not know they may not have the password so that's helpful but so now we got the key and if you look here go to the ssh we have it right here okay so this is the key that we just created and it has the private one right here and this is the dot pub so now what you want to do is you want to go ahead and copy the key over now if you search for a copy sh key there are different commands but the right one comes up from digitalocean and i i don't know why there's so many different commands but it's basically pb copy and this on the mac at least and then you just go right into the dot pub so i just saved it you know i could i just saved it on the side so i typed in the actual name right so if you want to change this to the name of your key and now we have it it's in your clipboard once you click enter and the way you can see that if you just literally try to paste this over i literally just pasted it right there and it's saved now what we want to do is go into your github we want to go into our private account so private you know into your profile your settings and then what you want to do is scroll down to ssh and gpg keys so i have one already saved so we're going to make a new one here the one that we just created now give it a title and i'm just going to paste the public key not the private key the private key never gets pasted anywhere it stays on your computer you only want the public key i'm gonna name this the same title just so i can reference it in the future all right and i'm gonna paste it here so i know i'll add the key um gotta write my password so i have the key it shows i haven't used it right okay cool now we have our key on our computer we have our our key on github before you go ahead and start trying to connect we want to tell our config folder that we want to use this key when we're connecting to github okay so right here there's a config file so you just want to go ahead and drag that over i have one for wp engine so i'm going to make one just for the one we just created called github all right so we're going to change this host url to github.com keep the user to get and now we're going to say preferred publication preferred authentication public key is fine and here i'm going to go ahead and say i want you to use this key when i'm connecting to github so that's the identity key i want you to use so github git so it's oh so i don't have to keep typing in hey before you push associate with this key and i have to update this to github.com it's very dangerous copying and pasting because it's very easy to miss things like this so let's say so now we should be host host name git git so that should all be right we're saying identity.com here and just as a note there already is a config file for every repo and it's associated so i'll just drag that in and you can see here you can make changes to this i like to keep my config file in the global area but it has its own inside of git its own config and you can see it has the remote origin it's going to have other data here and things like that here is where you can change your username um which is kind of helpful so what i'm going to do also before i do that i'm going to change the username so when it pushes it pushes from the right username so what you can do is do it per thing and this is basically going to us this is going to basically create the so it's going to change this config file if you actually put this command in so we're going to say git config and you'll see this happening actually let me delete this so i don't make any errors here right so now it's going to basically change it to let's say omar vaga save it so you saw that it made it it populated it right there and then when i go ahead and copy the next thing it's basically creating the syntax for you um but you can you know write that yourself but you are workspace.com and you're going to save it so you see here put the name and you could have just i just want to make sure it's pretty straightforward it's name email and straightforward so i've done it already saved now in the i commit it shows this is the user as opposed to grabbing something from your global area which is very common you might be pushing something personally to your work that's just an extra step you don't necessarily have to do i believe it should work after we added our key so let's say get let's just see if it's not giving an error get push great so now when we do get push everything is up to date now that we it's trying that means it's connecting it's not giving us an error the one thing i'm going to do is i'm going to make a subtle change just to see if i could commit something so let's go back i'm going to open up my my folder i'm going to make a change just so that i can commit something so you know i'll just change some css just so um we'll do like five let's change it to four just so that there's something there all right so now if we do get status we can see that we have a modification all right so what i'm going to do is go ahead and get add that hit add we're going to git commit m and say you know css font size and you definitely want to put some really good descriptions i know as like a developer we don't want to do that but you really want it's going to help you remember what was there so it got committed into our local repo now we want to push this so let's try git push and that's it this is what i love about you know github is really good with this is that this is going to be a little different if you set up your repo locally and then you make a remote one and then you connect them there's a little extra step but when you're cloning something um it's just get push the one extra step if you were to add this to the remote origin would be it would ask you to set like an upstream which is just follow the prompts it'll tell you when you do get push it'll say oh you're missing this one thing it'll have you add one extra step is like set the upstream url and when you do that it get push will work from there on so that's it so now if we go to if i refresh if i go over here to my repo there it is so if you go to commits you can see the latest one right there github is a little different than bitbucket bitbucket will list all the commits and which i kind of i like that about um bitbucket but now now i want to show you how to create something locally then push it connected so now let's do that so let's say we started off and we made you know a repo so i'm gonna go ahead i'm gonna create an empty repo so we're gonna make um empty repo sorry let's push this into our vs code okay so now we have an empty repo let's just put like a readme dot d all right just so we have something and let's make the actual local repo so we're going to say get in it done that so now we have a repo we'll again config but you can also just type here user you know i'm familiar with it now since we did it in the last email is going to be equals whatever this is just an extra step you don't have to do i just have multiple things going on on my computer so i always want to make sure i got the name so now what we're going to do is we can do git add git commit first commit now we're committed locally but this this repo is only local right now we want to get it on our github so now if we go to over here and i just go ahead and create a repo so go plus new repository and i'm just going to name this empty repo and i'm going to delete this later on empty repo could be public could be private i'll just make this private for now and then we're just gonna go create repository so we're gonna go ahead and save it cool so now we have a url it's all right here we can send anyone to that repo if it's private they won't get access we want to do is we want to connect the remote to the local and then push so what you want to do here is we've already created this locally so now we're going to say git remote add origin and origin is going to basically be the origin url the remote url but now we're going to go over here let's move this up and now we're going to say we're just going to paste that in all right so it's done so now it's added you have no error message it's added the local repo and it says hey the remote origin is the one we just copied on you know from github now if we do get push here's what i was telling you is that now this is the extra step that's going to say you have to set it up stream saying that now this is where the upstream url you want to do this doesn't happen for bitbucket it's more particular for github so you're going to copy and paste that once and and then you never have to do that again for the repo done it's actually set the repo now if we just do git push it's going to send everything oh that actually looks like it already pushed it so the setup stream actually did it all in one step but i want to show you if you make another change another change you'll see here and i save this if i do git add git commit m second commit or whatever just because just for the sake of testing i want to show you this now we've done a second now if you just do git push from here on out it actually just pushes you don't have to set up stream now if we go back to our repo hit a refresh and you're gonna see everything there we actually have everything right there if you go to the commits you'll see that there's two commits nothing really is in there if you go to the repo it's just like an empty readme and that's it so i just want to show you how to actually do that um i know when i first started this was super difficult so i hope you guys like it i'll see you guys next video
Info
Channel: Digital CEO
Views: 24,170
Rating: undefined out of 5
Keywords:
Id: J1BCa0N7Z7c
Channel Id: undefined
Length: 13min 50sec (830 seconds)
Published: Mon May 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.