Welcome to part 2 of this tutorial where we go from zero experience to professional level of git in one tutorial. In this section we're github and we're gonna learn how to use git and github to recreate the features of google drive's backup and sync. In order to follow along with this section you're going to need to know the basis of git and its version history. We go over that in part one, if you need to watch part one, you can find a link to that in the description. With that said, let's get started with part 2. Now we're going to begin the second project of this tutorial, we're going to create an online backup of our code using git and github. So the reason we do online backups is that, if you're coding and you just save everything on your computer, one day you might lose your computer or might get stolen or your computer just stops working. In that case you're just going to lose all of your work, and that's why we create online backups. There's already a lot of solutions that do online backups that you probably use such as google drive, icloud, and onedrive. So in this project we're going to recreate the features of google drive. Google drive allows us to do three main things: the first thing it allows us to do is to create an online backup of our code by uploading our code to google drive, so that we always have a copy of our code that we can download from online. The second thing that google drive lets us do is that it lets us download our google drive into a special folder on our computer like this, and then whenever we add stuff to this folder, it also gets updated on google drive. The third thing that google drive lets us do, is that when something updates on google drive, it will get downloaded into the special folder on our computer. So these last two features are known as two-way sync. Any updates on our computer gets updated on google drive, any updates on google drive get updated back to our computer. Those are the features of google drive that we're going to recreate in this project. We're going to do an online backup as well as a two-way sync for our code using git and github. Let's get started. You might be wondering: why do we use github? Why can't we just put our code into google drive? That will give us an online backup and a two-way sync automatically. And to be honest you're right. We could just put our code into google drive. But just like google docs version history, google drive is not specifically designed for git repositories. Just as a reminder, a git repository is a folder that contains our code, like we have here, that is being tracked for changes by git. Github gives us similar benefits to google drive, but it's specifically designed for git repositories. It has a lot of features that allow us to manage our code and our version history, and that's why we use a specialized service like github, instead of a generic online backup solution like google drive, and icloud. And there's actually several services that do the same thing as github. There's also bitbucket, and gitlab. In this tutorial we're going to learn github because it's the most popular one, but the other two services are very similar, the buttons are just in different places. If you learn github you'll be good with the other two. The first thing we need to do is to create a github account. If you already have an account you can skip to this time in the video. Otherwise we're going to create an account together. And don't worry github is free. So we're going to go into our web browser, and to github. com, and then we're going to click the sign up button, pick our username, and we're going to give an email and a password. And feel free to pause the video if you need, to I'm going to speed up some of the signing up process. And once we're done we're going to click create account. So feel free to skip all of this setup process that they have, we're just going to go to the bottom, and click complete setup. And then we have to verify our email address. So we'll go ahead and do that. And that's it! Now we have a github account and we're ready to learn how to use github to create an online backup of our code. In google drive, creating an online backup is actually really simple. We just have to drag our code into google drive, and that will create an online backup. How do we do this using github. What we're going to do, is we're going to create a new repository on github, and that's where we're going to save our code. To create a new repository on github, we're going to click our profile icon here, and we're going to click your repositories. And then we're going to click new. So now we have to give our new git repository a name. I'm just going to call it git dash tutorial to match the folder on my computer, you can give it another name if you want. And since we're learning, we're going to make this a public repository, that means
Segment 2 (05:00 - 10:00)
anyone on the internet can see this repository, and we're going to keep all the default options. And the last thing we're going to do is click create repository. Now we have a new git repository on github. So if you notice we now have two repositories. We have a repository on our computer, this is called our local repository. So anytime you talk about something on your computer, we usually call it local. So this is a local git repository, our local branch, our local version history, and we also have this repository on github. This is called the remote repository. So anytime you talk about something online we call it remote. So we're going to learn how to upload our local repository into this new remote repository. To do that, we're going to open our command line, which is terminal on mac or powershell on windows, and remember the first thing we need to do when we open the command line is we need to change the folder, or directory, that the commands are running inside of to this folder that contains our code. We're going to give this command: change directory tilde slash desktop slash git tutorial, and now the command line is running inside of this folder. The next step we need to do, is we need to add a link between our remote repository and our local repository, and we can do that with a git command. As a reminder, don't worry about memorizing the git commands I'm going to teach you because all of the commands in this tutorial are in a cheat sheet in the description below. We're going to use a git command to add a link to our remote repository on github. The command for that is git remote, and we're going to add, and now we have to do two things: the first thing is we have to give a sort of nickname to this remote repository. So you can give any name you want, but the convention is to call our remote repository: origin. And the second thing we have to give to git is the url of our repository. So we're going to go in here, and make sure that https is selected. If the url starts with git, make sure that you switch to https. We're going to copy this url, and we're just going to paste it into our command here, and we're going to press enter. So now this local repository has a link to our remote repository. And to check that, we can give this command: git remote. And this command gives us a list of remote repositories that are currently linked. So right now you can just see the name. If you want more details such as the url you can also give git remote dash v. Dash v stands for verbose, it just means give us more details. I'm going to press enter and now you can see that we have both the name and the url of the remote repository that's linked. If we ever want to remove a link to a remote repository, it's very straightforward, we just have to do git remote remove, and we have to give it the name of the remote repository we want to remove. So we're going to remove origin. I'm going to press enter, and now if we run git remote dash v again, you'll notice that we no longer have any remote repositories linked. So I'm going to re-add the link to our repository, feel free to pause the video if you want to do it yourself, to get some practice. git remote add and the name, origin. I'm just going to copy and paste. git remote dash v, to verify that everything is working properly, and that's it. Now we have linked this remote repository to our local and we're ready to upload our code. When we're uploading our code to github, this is known as pushing our code to github. Later on when we learn how to download our code from github, that is known as pulling so there's push and pull. We're going to start with pushing our code first. The first thing we need to do before we start, is we need to configure git with our github username. Right now the only person that can push to our remote repository, is the person that created it, which is us. So we have to tell git that it's us. To do that, we're going to give this command: git config --global credential dot user name, and we're going to give git our username. So I'm going to put my username, make sure that you put yours. Super simple dev dash test two, and now to push our code to github, we just have to run this command: git push and we have to tell git two
Segment 3 (10:00 - 15:00)
things: the first thing we need to tell git is which remote repository we want to push to. So to our github repository, and remember that earlier we linked our github repository and we named it origin. So we are going to push to origin. And the second thing that we have to tell git is which branch of commits that we are going to push. So let's actually take a step back and review what branches are. So if we run git log dash all dash graph to take a look at our version history, or our commit history, you can see that we have a series of commits. So we call this a branch and the reason we call it a branch is, remember that the git history can actually branch off. So right now we only have one branch of commits and this branch has a name. Mine is called master, depending on which version of git you have, it might also be called main. When we're pushing code to github, we're not just pushing our code, we're also pushing our commit history. And we don't just push our entire commit history, we push one branch of commits at a time. So that's why we have to give git push a branch name. Now let's go back into our command line, and press q to quit the git history if you need to, and now we're going to run git push again. git push, we're going to push to origin, which is our github remote repository, and we're going to push the branch master, or depending on your version of git, main. And now we're going to press enter to run this command. Github is going to ask you for your password. On mac we can just enter our password, on windows you might get this login pop-up, and all you have to do is log into your github account. So I'm on a mac here, I'm just going to enter my password. And once I enter my password, everything goes through. We pushed our code to github. We can go to github, refresh the page, and now you can see that we have just uploaded our code, and created an online backup of our code. So I'm going to do some troubleshooting right now if git push didn't work for you. If it did work for you, you can skip to this time in the video. So usually it's an authentication problem, and there's several things that we can do. The first thing that we can do is we can run git remote v, so we have to make sure that the url to our remote repository starts with https, and it doesn't start with git. If it does start with git, then we can go back into our github repository, and we can get the https url here. So make sure that you're not in the ssh section, we're going to learn more about ssh later. So copy and paste this. What you need to do is git remote remove origin, and then git remote add origin. We're going to re-add it with the https url, and then try git push again. If that doesn't work for you then double check that you have configured your github username. If we go back up, remember that we configured our github username here, so make sure that you run this command, and you type it out exactly as I have it here, and you use your github username inside the quotes. And then if that doesn't work for you, another thing you can do is run git remote -v. So we're going to replace this url, we're going to run git remote remove origin, and then we're going to re-add the url but we're going to add to something extra. So let's go back into github and copy this url, and paste it. And we're going to go right before github. com here, and you're going to enter your github username. So I'm going to enter mine make sure you enter yours. super simple dev test 2 and we're going to put the at (@) symbol so it's going to be your username at (@) github. com blah your repository. And if that doesn't work for you, the next thing we can try is to create a personal access token. So we're going to go into github here, we're going to scroll up, and we're going to click our profile icon. And we're going to go into settings, and we're going to scroll down into developer click the personal access tokens section. And we're going to generate a new token. So go ahead and enter your password, and now give a name for your token. So I'm just going to call this access token and make sure that you check this repo checkbox. So this is going to give you permission to push to your repository. Let's scroll down, and we're going to create a personal access token. So make sure that you copy this and you save it in a safe place. You can't copy it anymore once you go out of
Segment 4 (15:00 - 20:00)
this page. I'm just going to copy this in my code editor, put it in secrets. txt, and now we're going to run git push origin master again, and when it asks you for your password, instead of giving your actual password, you're going to copy paste this personal access token, and that should work for you. If that still doesn't work for you, the last resort we can do is to set up ssh keys, and you can skip to this time in the video to learn how to do that, and then come back to the section of the video once that's done. So we just uploaded our code to github, now we're going to take a look around github to see what kind of features they have. So you can see that we have an online backup of our code here, we can look inside our files and folders just like we would in a code editor. Let's go back into our repository, and you'll see that git also gives us more information about each file and folder. It tells us the latest commit where this folder or this file was changed, it also tells us how long ago this file was changed, and we can also click here to see our commit history in our web browser. So you can see that this matches up with what we have in our command line. So the cool thing about this is that you can also go into each commit, and this will show you the changes that occurred within each commit. We can go in here, and see that we changed some lines of our code and it makes it really easy to see what changed. And the other thing that we can do is we can click browse files to see all of our files and folders at a specific commit. Essentially we're going back in time to see a previous version of our code. So remember in the command line, the command for doing that was: git check out a commit hash. Well in github you can actually do this in your browser, so it's a lot easier to see. So you can see that unlike google drive, github is designed specifically for git repositories. It gives you a lot of visibility into your code and how it's changing, and that's why we use github instead of a generic online backup service like google drive, or icloud. That's how we create an online backup of our code using git and github. Next we're going to learn how to create that two-way sync feature of google drive for our code. For google drive you can actually download your entire drive into a folder on your computer, so then every time you add a file to that folder, or you modify a file inside the folder, the changes will be reflected online in google drive. We're going to learn how to recreate that feature using git and github. Before we begin, we're going to learn how to visualize where our local and remote repositories are, to see if anything's out of sync. The first thing we'll need to do is, let's make sure that we're in the root of our github repository, and then we're going to open our command line, and we're going to do git log --all --graph And now you can see that we have another branch here, or another branch name, called origin slash master. This is called a remote tracking branch. So remember that we pushed our entire branch up to github, and now github essentially has a copy of our branch. And that's what this remote tracking branch is here to help us with, it tells us what the master branch, or the main branch, looks like on origin, which is github. Right now you can see that the master branch on github, which is origin slash master, is the same as our local master branch. And that's because we just pushed our master branch up to github so they should be identical. Now let's see what happens when we create a new commit in our local repository. Let's quit out of this git history by pressing q if needed, and now we're going to create another commit. If you want to practice feel free to pause the video, but we're going to create a commit together now. So I'm going to go into my code and I'm just going to create a commit here. I'm just going to call this version 11. And I'm going to create another change here as well. Remember we have to pick which changes we want in our next commit using git add, I'm going to use dot, and we're going to create a commit using git commit -m version 11. Now that we just added a new commit to our local repository, let's look at our git history again. So git log dash all dash graph, and now you can see that the master branch in our local repository is actually ahead of the master branch in github. So that makes sense because we just created this new commit that hasn't been updated to github. So how do we update these changes from our local repository to our remote repository? Well we actually already know the command so let's get out of the git history, and we're just going to run git push origin master again, and now if we go into github and refresh the page
Segment 5 (20:00 - 25:00)
you can see that version 11 has now been uploaded to github, and if we go back into our command line and run git log dash all dash graph, we can see that origin slash master is now up to date with our local master branch. So we use git push in both cases: to initially push our code up to github, and then later on to push new commits up to github as well. For the rest of the section we're going to learn a little bit more about git push. So let's go back into our command line, and now if we run git push origin master again, you'll see that it tells us everything is up to date. So if we look back at our history, we'll see that origin slash master is in sync with our master branch so there is no updates to push to github, and that's what it's telling us here. So now I'm going to show you how to set up a nice shortcut for git push. If we run git push origin master, we can also give it dash set dash upstream. So what this does is that it sets up a sort of shortcut for git push, so that the next time you run git push, you can just run this command, and git will automatically memorize origin and master for you. So that's a nice shortcut that you can set up. The next thing we're going to try, is we're going to make some changes to our code. So let's go into our code editor, and I'm going to change this to version 12, and 12 as well. And now if we run git status, you'll see that we have some new changes to our code, and if we run git push origin master again, you'll see that nothing happened. So why is this? Well git push only pushes commits up to our remote repository, so it github. If your changes are not in a commit, then git push won't be pushing anything. Even if we do git add dot and we add these changes to our staging area, so let's run git status again just to make sure so these changes are now in our staging area, even if we run git push here, you'll notice that because these changes are not in a commit, remember that git add only picks which changes we want in a commit, it doesn't actually create a commit. If these changes are not in a commit, git push will not push anything. git push only pushes commits, it doesn't push changes. Now the final thing I want to show you is, that we're actually going to create a commit here, but instead of creating a new commit, we're going to edit our previous commit. So remember the command for doing that is git commit dash amend. So amend, instead of creating a new commit, it's going to overwrite the previous commit, and we're going to give it a message. So let's just call this version 12. And now we're going to run git log to check our version history. So you see that our version history actually branches off. We have one and one branch here, and the reason this is happening is because in github the master branch still has this old commit, while on our local master branch we've essentially gotten rid of this old commit, and we overwrote it with this new commit. So that's why there's this branching happening in our git history. And now if we run git push origin master, we're essentially going to overwrite the commit in our master branch in githu, b with this commit in our local master branch. So let's go ahead and try to do that. git push master, and you'll see that git actually doesn't allow us to do this. And the reason for this is because if we git push, we're essentially going to overwrite and replace this commit so, we're going to lose that commit. It's generally bad practice to overwrite commits that you have already git pushed to a remote repository, but since we're learning we're going to learn how to get past this warning. So the way is to run git push origin master again, but use dash f so dash f means force push, it means to push this commit to github even if it will overwrite a commit on the github branch. So that's what you have to do, if you ever mess up your git history and you need to overwrite github. Now let's go back and run git log again, just to check our commit history, and now you'll see that origin slash master is now in sync with our local master branch, and origin slash master has this version 12 commit, and essentially replaced the version 11 command that we had earlier. So that commit that we replaced is now lost, and we can also check in github as well. So let's go back into github, and refresh the page, and you'll see that we now have the version 12 commit. And if you look at our commit history on github, you can see that there is no version 11 commit, we essentially replaced it with this one. So in this section we learned how to sync updates from our local repository up to
Segment 6 (25:00 - 30:00)
our remote repository, as well as some extra features of git push. The last feature of google drive we're going to recreate is when we update something in google drive, online those changes will be synced back to our computer. We're going to learn how to do the same thing with our code using git and github. Whenever there's an update to our github repository, that's not in our local repository, we're going to learn how to sync those changes back to our local repository. To set this up, we are actually going to pretend that we are on a different computer, and to do that we are just going to re-download our github repository into a different folder. First let's make sure that we are in the root of our github repository, and we're going to open our command line, and we're actually going to open a new window, and what we'll do is we're going to change the folder or directory that the command line is running in to our desktop. And now to download a copy of our git repository from github, we're going to give this command git clone. So this is going to clone or copy our github repository onto our computer, and the way that this works is that we have to give the url of our remote repository. So to do that let's just go in here, and click this download code button, and then copy this url, and we're going to paste that to git clone. And we also have to give a folder name so, you don't name. If you don't give a folder name, it'll create a folder called git dash tutorial in the folder that your command line is running in, which is the desktop. So it's going to create a folder called git-tutorial on the desktop, but you'll notice that we already have a folder called git-tutorial, that's why we have to give another name for our git repository. So let's name this new-git-tutorial, and press enter. And now you can see that we downloaded our github repository and saved it in this folder on our computer, and now we essentially have two local repositories, the old one and the new one. And these two are completely isolated from each other. Even though they have identical commit histories, if you update the commit history in this new repository, it won't affect the old repository. This essentially mimics what it would be like if we went on a different computer, and downloaded our github repository on that computer, and started updating the commit history. The next thing we're going to do, is we're going to go into our new repository, we're going to create a new commit, and then push that to github, and then we're going to learn how to sync those changes back to our old repository. So remember we already know how to do this, and we'll do it together. The first thing we need to do, is remember the command line needs to be running inside the folder that contains our code. Right now the command line is running inside the desktop, so we have to change directory to tilde desktop slash new git tutorial, and now let's make some changes to our code. So our code editor is actually opened to our previous repository, so we have to close this, and then reopen the code editor to our new repository. So once that's open I'm going to go in and make some changes, so S'm going to change this to version 13, just make some simple changes, and we're going to go back into the command line, and we're going to run git status. So now we see that we have some new changes, So we're going to run git add dot to add these to the staging area, and then create a commit. Just going to call this version 13. So now we have a new commit, and to push this commit to github, we just have to run git push origin master, like we learned. So now we can check that worked using git log --all --graph and you can see that our local master branch has a new commit called version 13, and the master branch on github, so origin slash master, is updated to include this version 13 commit. So now we're going to learn how to sync these changes back to our old repository. First let's check our old repository in the command line. So if you still have this command line window open you'll notice that it's running inside the folder that contains our old repository. If you closed this window previously, you can reopen your command line, and then remember to change directory back into this folder. So let's go into our command line, and we're going to run git log --all --graph and we would expect to see a new commit on github, but you can see that the new version 13 commit is not here. It also seems to be saying that origin slash master, or github, is in sync with our local
Segment 7 (30:00 - 35:00)
master branch, and we know that's not true, because we actually pushed a new commit to origin slash master, so it should be ahead. What's going on here? Well the reason this is happening is because these remote tracking branches don't update automatically, so this is not reflecting the actual state of the master branch in github, it's just reflecting the state that it was in the last time we updated it. So to update all of our remote tracking branches, we're going to go back into our command line, press q if you need to, and then we're going to run this command, git fetch, and this will essentially update all of our remote tracking branches to the current state in github. And now we can run git log again, see that our origin slash is ahead of our local master branch, so this is what we expect. And now we're going to learn how to sync this new commit back into our local repository. And to do that it's very simple. So let's go back into our command line and to pull changes from github, we're just going to run git pull, and this works the same way as git push. We're going to tell git which remote repository to pull from, so we're going to pull from origin, which represents github, and then we're going to tell git the branch that we want to pull. So we're going to pull master. We're going to get any updates on the master branch from origin and we're going to sync them back to our local master branch. Now let's run a git log again to check that it worked, and now you can see that our local master branch has been updated and is now in sync with what is on github. So the last thing I want to show you about git pull, is that you can also set up the same shortcut that we did with git push. So we run git pull origin master and then dash set up stream, now we can run git pull and git will essentially memorize origin and master for us, and now git pull will be the same thing as running git pull origin master. So that's another nice shortcut you can set up. So if you run set upstream, this will set up the shortcut for git push and for git pull, so you only need to run this for one of them. And that's it! We finished recreating three main features of google drive using git and github, we learned how to create an online backup of our code, we learned how to sync updates from our computer to github using git push, and we learn how to sync updates from github back to our computer using git pull. One thing you'll notice is that all of the git commands we run have to be done manually. For google drive, when you change something on your computer, that change will be synced automatically online, and when you change something online, those changes will also be synced automatically back to your computer. Now why do we do things manually with git? It's the same thing that we do things manually for the version history, we don't want our code to be automatically updating unexpectedly while we're writing our code. We want to make sure that our code still works, before we're ready to update it, so if you're writing code and things are syncing automatically, there's a high chance that you get all these unexpected updates, and your code will stop working. So that's why, for git, we always do things manually. At this point in the tutorial, we're comfortable using git and github. Now we're going to look at some common scenarios where we use git and we're going to see how we handle those scenarios. So this section is also a good exercise for you. What you can do is: I'm going to tell you the scenario, what we want to do, and you can feel free to pause the video and try it yourself, and figure out the git commands before we go through it together. The first scenario we're going to go through is: let's say that we have an existing project on our computer, and now we want to integrate git into this project, and we want to upload it to github. This usually happens when you've been coding for a while so you have some projects, but you just learned about git, so there is no git involved in your projects. Let's say that we're on our computer, and we have an existing project, and that project is contained inside this folder. So inside this folder I have a file of code and it just contains that, so let's say this is our existing project, you can pretend that you have 20 different files in this project, or you can even use one of your own projects. How do we integrate git into this and then upload it to github? So feel free to pause the video right now and figure out how to do that. Otherwise we're going to do that together. Here's the answer and we'll go through it together. First we'll run git init inside this folder, which will turn it into a git repository, then we're going to create a commit, and repository on github, where we're going to upload this project, and then we'll run git push. Let's go through that together. So we'll open up our command line, remember we have to run inside the folder that contains our code
Segment 8 (35:00 - 40:00)
so let's change directory into that folder, and next we're going to git init. So this will set up git, and now git is tracking our code, we can run git status to make sure that the code is being tracked, and then we'll create a commit like we've been doing so far. Once we have a commit we're ready to upload to github, we just need a repository on github to upload to. We'll go into our web browser and we'll go to github. com and then we're going to go into our repositories. We'll create a new repository where we're going to upload this project. So feel free to give any name that you want and to keep the default values. I'm going to create this repository. Next, remember we have to link this remote repository to our local so that we know where we're uploading to. So to do that we're going to open our command line again, we're going to run git remote add, and we have to give two things, the first thing is a name, so typically we call this the origin, and the second thing is the url of the repository. I'm gonna go with the https url, we're gonna learn about the ssh url in the next section, so for now if you're not familiar with ssh, use the https url, and once we've linked the two repositories together, now we just have to run git push origin master to upload our code into github. So we refresh the page, and that's how you go from an existing project that doesn't have git, to tracking your changes with git, and uploading it to github. The second scenario we're going to do for practice is, let's say that we want to start a new project, and we already know about git and github. So we know that we want this project to be on github, how do we do that? Feel free to pause the video and figure out the steps yourself. Otherwise we're going to do them together. Now there's actually two ways to do this, so the first way is how we've been doing it in this tutorial, we're going to create some code, a repository on github, and I'm going to link the two together, and then upload it to github. The second way we're going to do this is we can actually start by creating the repository on github first, and then we're going to download it onto our computer using git clone, and then we'll start developing as usual. Let's see how we use this new method. We're going to go into our browser, and create a repository first, so we'll go to github, and go into your repositories, and create a new one, give it a name. OSnce we have our github repository, we're going to first clone that back to our computer. To do that we're going to open our command line, and we're first going to change directory into our desktop. The reason we change into our desktop is that when we run git clone, it's going to our remote repository into the folder that the command line is currently running in. So if we're currently running inside the desktop, it's going to clone this repository right here, or here. So let's go ahead and run git clone. So we have to tell git the url of the repository that we want to clone, and that is right here, we're going to use the https url, I'm going to copy that and paste it to git clone, press enter, and now you can see that we downloaded our fresh new github repository onto our computer here. And now we just develop as usual. So we can make some changes, add some commits, and then git push to github. So one of the reasons we do this is that if we change directory into this folder that is our new repository, and you run git status, you'll notice that we already have git inside this folder. So usually if you start a project on your computer, and then upload it to github, you have to run git init. And also if we run git remote -v you'll notice that when we do git clone, we already have a link from the remote repository to the repository that we cloned, so using git clone helps us save some setup steps for git. And that is how you handle creating a new project once you're already familiar with git and github. The third scenario we're going to take a look at is probably the most common one when we're on the job, so let's say that we just joined a new team, and that team already has a project that's uploaded on github. So how do we download that project and
Segment 9 (40:00 - 45:00)
start contributing to the code. So feel free to pause the video right now, and try to figure out the git commands for doing this. Otherwise we're going to do it together. So the steps for this scenario is that first we're going to clone the your team's repository to our computer, and then we just work with it as if it's a normal repository that we created. So we can just start adding code, we can create a commit, and then we can git push back to our team's repository. Let's go through this together. So we're going to go into github, and we're going to pretend that we just joined a new team, and this is the team's project, so the first thing we're going to do is we're going to download this project onto our computer, and then we're going to work with it. So let's go here, and so we already know how to use git clone, we're just going to practice by doing it again. We're going to change directory to our desktop, and then we're going to run git clone again, and we have to tell git the url of the repository we want to clone. So let's go in here, we're going to click this download code button, and then click copy. I'm going to paste it here, and one thing to remember about this is that git clone will create a folder with the name of your repository, which is git-tutorial but we already have a folder on our desktop that has the same name, so we have to give it an alternate name. So let's call this team project, and now we just downloaded our team's project onto our computer, and we just work with this as if it's a normal git repository. So we would go in, make some changes, create a commit, and then git push, and that's how you would handle joining a new team, downloading the project, and then learning how to contribute to their project in github. In this section we're going to learn the proper way to set up our github credentials. You might be here from earlier in the tutorial, if you had trouble running git push, or you might be here after finishing the rest of the tutorial. What we're going to learn is how to set up our ssh keys. The way this works is that instead of giving our password to git push to a github repository, we're instead going to create what's called an ssh key. So this key is simply just a file that we create on our computer, then whenever we run git push we're going to pass this key to github, and this key is going to give us access to push to our repository, instead of us using our password. To set up our ssh keys, we're going to go into our web browser, and we just have to google github set up ssh key, this will take us to a guide on docs. github. com and we just have to follow this guide. Now there are different guides depending on your operating system, I'm going to start with mac first, windows users you can skip to this time in the video. For mac, we just have to follow this guide, so first we're going to check if we have existing ssh keys. If we don't we'll generate a new one and then we'll follow the rest of the guide. To register our key in github, let's go into the first link and we'll do this together. For most of this guide they just give us some commands to run, so we're just going to copy and paste this into our command line, which is terminal, and then just run these commands, and follow their instructions. So first we're going to open terminal and we're going to run this command, so I'm just going to copy and paste it into terminal and run it, and then we have to check if we have any of these files, so I don't have any files here, that means I don't have an ssh key already. So the next thing we have to do is to generate an ssh key. This second step is the same thing as the first one, they're going to give us some commands that we just have to copy paste and run in our command line. Let's go down here, I'm going to copy paste this first command, and then run it, for this command make sure that you change this email address to the email that you register to github with, so the email that I used is this one make sure you use your email, and then it's going to ask us for some information, so we can actually skip past these steps by just pressing enter. Now we're going to create a new password for our key, we're going to enter that password again, and this essentially generates our ssh key. So we've done these steps here and now we have to add our ssh key to the ssh agent. So what we have to do is, again, we're just going to copy and paste these commands, and run them.
Segment 10 (45:00 - 50:00)
Now, for this one you can actually run this command first to create this file if it doesn't exist, and that will allow us to open it, and now we just have to copy and paste this content into here. We're going to save and close it, and now we have to copy and paste this command. So most of this guide is just copy and pasting commands into the command line, and then running it. And now the last step is to add this ssh key to our github account, so we created an ssh key earlier we're just going to register this ssh key to our github username so that we have access, and now you notice we're back to the original sort of search result that we were on, and now we just have to follow these steps. So it's more copy pasting and running it in our command line, so we're just going to copy and paste this, and then run it, and now we're going to go into our settings, and find ssh and gpg keys. So let's go into github. com, click our profile icon, click settings, I'm going to find ssh and gpg keys, I'm going to add a new ssh key, I'm going to give this a title, just call it my ssh key, and we're going to essentially paste in here. So what this command does is that it copies the contents of this file into our clipboard, and that's how we can paste this into github. com, and now we're just going to add our ssh key. So this registers this key to our username and now we have access when we git push. And that's it, we just added an ssh key to our github account, and now you can skip to this time in the video to learn how to use it. For windows, setting up our ssh keys can be a little tricky. That's because this guide is written for git bash, while we're using powershell, which can be a bit quirky. So we're going to go through this guide together, I'm going to give you the equivalent steps for powershell. First we're going to go into this link, we're going to check if we already have ssh keys, and we're going to run this command in powershell without the -al so let's copy this and paste it here, we're going to remove dash al and run this command. So the point of is we want to make sure we don't have any of these files in the output here, so you can see that we don't have these files, that means we don't have an ssh key, and our next step is to generate a new ssh key. If you already do have these files here, that means you have an ssh key, you can skip to this step if you want to, so let's go into this link here, to generate a new ssh key, and for powershell, we can copy paste this command into here, and just make sure that you change this email to the email that you signed up with github. Now it's going to ask us where we want to save our key. We're going to keep the default by pressing enter. Now we're going to pick a password for our key, and we're going to enter that password again. All right, so that step that we just ran created a new ssh key, and it saved it in here. Our next step is to add our new ssh key to the ssh agent, so for windows the steps to starting the ssh agent is a little different, we're going to go into our start menu, and we're going to search for services, and once that opens we're going to look for open ssh, so this is alphabetical we're going to look for o open ssh authentication agent, we're going to double click this, and now to start our ssh agent we're going to go into this drop down, I'm going to click automatic, I'm going apply, and then we're going to click start. So the ssh agent manages our ssh keys and it makes sure that when we git push we're using our keys to authenticate to github, so now we can press ok since our agent is running. We can close
Segment 11 (50:00 - 55:00)
the last step is to run this, now we're going to need some modifications to this command. So we're going to change this tilde to $HOME I'm going to press enter, and essentially we just added our new key that we just created to our ssh agent. Now our last step is to add the ssh key we just created to our github account so that github recognizes this is our key. So we already did these two steps, we can skip them and go down here, so instead of doing this we're going to do something different for powershell. So let's copy this I'm going to paste this here, and we're going to run notepad and we're also going to change the tilde $HOME just like we did up here. Let's press enter, so we're going to take this. So this is what's known as our public key, we're going to talk about key pairs in another tutorial. So we're going to copy this, and now we're going to go into github, go into settings, ssh and gpg keys, and we're going to add a new key. We're going to go into here, I'm going to go into settings, and we're going to find ssh and gpg keys. I'm going to add a new ssh key, so give this a title, I'm going to call it my ssh key, and I'm going to paste the contents from this file, and then I'm going to click add ssh key and that's it, we created an ssh key, and we registered it to our github account. Now we're going to learn how to use this ssh key when we git push. Now that we've set up our ssh keys, we're going to learn how to use them. We'll start off by opening our command line, and for this lesson we're just going to use this git repository that we've been working on for this tutorial. So first, let's change directory into this folder, so that the command line is running in that folder, and now let's run git remote -v So to use our ssh keys, remember that we had two urls for our remote repository, we have the one that started with https, and we have another one that starts with git. So to use our keys, we just need to switch this url for the git url. To switch our urls, first let's remove this https url. To do that we're going to run git remote remove, and we're going to remove origin. Now if we run git remote dash v again, we no longer have any remote repositories linked to our local repository. Now let's get that git url. So we'll go into our web browser, we're going to github. com, and let's open up our repository on github. So it's this one and we're going to click this download code button, but this time we're going to select the ssh url, and this one's going to start with git. We're going to copy this one, and go back into our command line. Now we're going to link our remote repository again, except this time we're going to use the ssh or the git url, so git remote add, we're going to call it origin again, and we're going to give this url. Press enter, now every time we git push, it's going to send our ssh key to github instead of us entering our password. To test that out, we're going to create a new commit, and then we're going to git push again. If you want to, you can pause the video to practice yourself. Otherwise we're going to do it together. So let's go into our code editor, we're going to open this repository in our editor, and make some changes. So I'm just going to add a new file called ssh. Just a simple change to practice with. Next let's run git status, to see that this change is being tracked by git, and now we can create a commit. git add dot and git commit m use ssh keys, and now we just have to run git push origin master again, and because we're now using the ssh url, it's going to send our ssh key along with git push, and that's going to give us access to our repository.
Segment 12 (55:00 - 56:00)
And that's it! we learned how to set up ssh keys to access our repository instead of using our password. This is the proper way to set up our credentials for github and this is probably one of the first or second things that you do when you set up git on a new computer. And that brings us to the end of the second project, we learned how to create an online backup of our code, we learned how to push changes from our local repository to our remote repository, and we learned how to pull updates from our remote repository back to our local repository. In the next section of this tutorial, we're going to learn the last skill that's going to take you to that professional level, which is branching and merging. Thanks for watching, my name is Simon from supersimple. dev my mission is to make up tech career possible for anyone. If you have any comments or questions, please leave them down below. Remember there is a cheat sheet in the description, and you can always contact me at supersimple. dev/feedback I'll see you in the next one