Learn Git in 20 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody whether you're a longtime programmer or just getting started you have probably heard that get is one of the most important skills any developer can learn and I completely agree this is why I created this video to tell you exactly why it is so important explain how it works and most importantly show you everything you need to know in order to get started with it in only 20 minutes let's first start with what git is git is a version control system more specifically a distributed version control system and makes tracking changes to your code over time painless git tracks every change that has ever been made to the code you tell it to manage and let you easily switch between versions of your code or even undo changes it also makes it easy for teams of people to work on the same project and even the same file since each team member has their own version of the code saved locally on their computer it also saves the full version has drowned each team members computer which is what makes it a truly distributed version control system this is all great but teams need a place to store their code remotely so that all the team members have a central place to save their changes to and pull down other team members changes this is where github comes in many people get confused when they first learn about git and github thinking that they are the same thing however github is just a website where developers can store their projects for other developers to access get on the other hand as we talked about is just a version control system for tracking and managing changes to code now that we have a basic understanding of what git is let's jump into why git is so important and why you should be using it on all of your projects imagine the scenario where you're working on a multi-part youtube tutorial to learn the basics of HTML such was my introduction to HTML course and you just finished writing all the code for one of the middle videos in this series only to find out that something you did broke everything without get you would have to resort to trying to remember what you changed since the previous video and then search through each of those files to determine what exactly is wrong if you're lucky you'll remember all the changes that you made but most likely you'll have to go through all the files of the project looking for a single mistake with git though this is a trivial problem to solve since git tracks all the changes that you make you would simply have to tell git to show you all the changes that have occurred since the previous videos code and git will not only show you what files have changed but will also show you the exact that have occurred in each file this makes finding what went wrong more enjoyable and quicker get also allows you to change back to previous versions of your code with a single command this is extremely useful for any project since if you deploy your changes to the world and realize later that something major is broken you can simply tell git to change back to the previous non broken version of your code without get this is an impossible task since nobody will perfect remember all the things that need to be reverted to get back to the previous version lastly git is used by nearly all developers with Stack Overflow or partying that nearly 90% of their 75,000 developers they surveyed use git as their version control system this means for anyone looking to get a job in programming they will need to know get lastly you need to ensure that git is installed on your system and configured with your github account if you do not already have git installed or do not have get linked with github then check out my video on the introduction to web development where I cover installing and setting up gate with github this will be linked in the description below now that you have get properly installed and set up with github we can begin talking about how to use git the first thing you need to do is initialize get in your existing project or clone a remote project from a website such as github in order to initialize get in a project that already exists on your computer simply run the git init command in the terminal from the folder that contains all of your project files this will add a dot git folder to your project that git will use to track all the changes you make to your project it also uses the word repository or repo for short to describe the entire project being managed by git the second option of cloning and repository can be done by going to github and either creating a new repository to clone or finding an existing repository and copying the link to clone the project if you set up git and github using the method from my video make sure to copy the SSH URL and not the HTTP URL now with that URL copied type the command git clone into the terminal with the copied URL at the end of the command this will copy all the code from the repository onto your computer and set up get to track the changes this also adds the remote repository to your git config for the project so that when you are ready to push your changes to github git will already know where to push them to now that we have a project set up on our computer let's talk about the structure of git git tracks changes through three stages the working directory index and head the first stage is the working directory the working directory is simply all the files in the local repository in their current state none of the changes in the working directory are tracked by git in order to track these changes we first need to add them to the index which is commonly referred to as staging this can be done by using the git add command this process of adding changes to staging tells gif that we want to track all the changes that we have made in our working directory this still does not make these changes in official version of your code that you can change between them in order to add these changes as an official version we need to scale get to commit these changes from the index to the final stage the head this tells get the changes are final and requires a message to be saved with the commit this can be done by using the git commit command the head is simply the most recently committed version of your code after committing changes to the head they are saved as a version on our computer but they are not in our remote repository on github in order to get these changes to our remote repository on github we need to push them to github using the git push command if you setup your project using git clone link it will already know where to push the files to but if you use to get an it to initialize a new project then git will need to be told where to push these changes to this can be done by creating a repository on github and copying the ssh URL for cloning after that is done you can simply use the git remote add origin command with the copied URL at the end of the command this tells get that we have a remote repository called origin at the copied URL with the remote repository added you can use the git push command to push the changes to the remote repository on github we now are able to create a repository make changes to it save them and push those changes to github but what if another one of our team members wants to pull down the changes that we push to github onto their local computer this is where they get pull command comes in by simply typing the command git pull into their terminal all of your changes from github will be pulled onto their local computer and mergers any of the changes that they have that is all you need to know about get in order to get started but there is one more big concept and get called branches branches are essentially a copy of the code at a point in time that has its own separate history from the main branch which is called master imagine you're in a company with two teams and one of the teams is creating feature a of a website and the other team is creating feature B for the same website these features will have multiple commits in order to be fully completed but if both teams are working out of the master branch then whenever the team working on feature a pushes their changes to github the other team will get those changes for feature a when they try to pull this will cause problems because now the team working on feature B will have parts of feature a in their code this is where branches come in if the team working on feature a creates their own branch off of master and the team working on feature beed creates their own branch off of master then when the team's push those changes to github they will only be pushing their changes to their own branch and not the master branch this can be done using the command git branch with the name of the branch being created at the end of the command this will create a new branch but in order to switch to that branch they get checkout command will need to be used with the name of the branch at the end of the command since this is such a common operation there is a parameter you can pass to the check out command to create a new branch and swap to it this command is get check out - B with the new branch named after the - B now that both teams have created their own branches and committed all their changes for each feature to their branches it is time for them to merge these changes back into master get makes is easy with the git merge command git merge followed by the branch name that you want to merge will merge all of the changes from that branch into the branch you currently have checked out if there are any conflicts between the two branches gate will notify you of these conflicts and let you decide what to do with them before committing the changes this is finally the end of all you need to know about git I know it can seem quite daunting but I'm going to go over a live example of exactly how all these commands work to give you a more hands-on experience with how it works let's jump into that hello everybody let's first get started by setting up a project on getting the easiest way to do this is to go to github create a new repository give it any name you want we're just gonna call ours github test and then click to initialize this with it readme this will make us that you can clone the project down to your computer and then click create your posit or it and after that's done loading or bring you to your repository page here's where you can cloned the URL and make sure you use the SSH URL if this says clone with HTTP make sure you click the button up here which will say use SSH after you're done with that click the URL copy it and then go to the folder that you want to copy this into once you're here you can right-click this and open the terminal here and then all you need to do is type git clone and then paste in the URL that you just copied hit enter for this will copy all the code that is inside of that repository which includes that readme we created and bring it into the folder that you are in currently with the terminal now to open this just right click it and open it with Visual Studio code and we can get started right away and we see we have a readme and it just says the project name the readme is important in version control systems such as github because if you go to github site you can see that inside of your repository everything in the readme will show up on the main page of your repository this is a great place to put notes and information about what your code does so that when people come to your repository they have an easy way to understand it without having to read the actual code now that we have that out of the way let's get started by creating a file in this project we can call it anything you want such as test HTML and hit enter after a little bit you'll see that this file will turn green with a little u next to it that stands for untracked this is because Visual Studio code integrates perfectly with git and this tells us that this file which is green means that it is new and since it has au st. its untracked that means we haven't added this file to our git repository yet in order to do that let's open up the terminal by holding ctrl and hitting the tilde key and typing in git add what we'll need to do is add the file name to the end of the git add command so I'll get what we want to add so we'll say test HTML this will add get the test HTML file to the index of our git repository when we hit enter we'll see that this turns to a instead of you which means it's been added to the index and then lastly if we want to commit this change we can type git commit and then we need to add a message to this so use - M to say that we're adding a message and then in quotes but whatever message you want such as added test file and then hit enter and this will add this file to our repository and you'll see that it is no longer green anymore because it is being tracked by git and is in an actual commit now let's say that we want to edit this test file by adding some constant in here let's just say we're gonna add a paragraph tag and we're going to put hi inside of it if we save that we'll see that this will turn yellow or orange which means that the file has been modified as you can see by the end this means that it is different than the current version in github or in get right now in order to see what's different between these two files we can run the git diff command this command will tell us everything that is different in the files that we have in our repository between the currently version that we have in our working directory and what is the most recent version in the head so if we hit enter we'll see that it tells us that this file the test dot HTML the only difference is that we added this line high which is what the green stands for it means that has been added now let's add that to a repository and instead of using the file name we're going to use a period which tells get that we want to add every single file that is in our project this is an easy way to add bunch of files at one time so when we do that we'll see that our file will get added here and then if we want to commit that we did get commit give it a message we'll just say a second commit hit enter and now our file will be added with git we can make as many changes to as many files as we want in the project and as soon as we add them they will all be added into the index we can even add multiple different times so let's say for example we want to add another P tag here and just say content and then we want to add that so we do get add and put the period at the end hit enter and now let's say we're gonna change it again before we commit and we want to put an end here so we can get add for the period at the end and now let's add it to the index as well so you can do as many as you want in between commits and now when we say okay we have all of our changes that we want we can say get commits send it a message of final commit and we hit enter we'll see if this file will no longer be marked as modified because is actually in the head of our project now that that's out of the way let's push our changes to github using the get push command if we have git and github configured correctly this command will automatically push all of our changes to a remote repository let's first look at our remote repository refresh it and see that none of our changes are currently here in order to get our change to just show up there we just need to run they get push command we hit enter now this will push all of our changes to our remote repository and if we head over to it hit refresh we'll see that these changes are now here and it shows us the commit measures that we sent with it as well now let's go back to here and get started on git branches the first thing we need to do is to create a separate branch for a new feature that we're going to create let's say that we are going to create a feature that allows us to take payment so we're gonna want to create a store so we use git checkout dash B and the name of our branch which we will just call store now we've transferred over to a separate branch as you can see in the bottom left hand corner here visual studio code tells us exactly what branch we are on which says store right here so we know that we're on the store branch now let's add a file in here called store dot HTML and let's add some content we will just add a paragraph tag in here that says this is the store now we're going to save that if we add that in and commit that by saying added store and now we want to push these changes let's do git push and we'll see that it says there is no branch currently on github for this project because we don't actually have a store branch already on github we need to do is we need to tell github to create this store in order to do that all we need to do is copy this command which will configure get in github so that it is able to pull and push to the correct branch so now that git and github both know that this branch exists let's paste that in here and hit enter and we'll see that it'll push our changes up to our remote repository now let's go over to our remote repository refresh it and you'll see that these pages don't actually show up it's because it's on a separate branch as you can see here we are on branch master and if we switch it to the store branch you'll see that now our store changes show up here we switch back to master you'll see that they are no longer there but we want those changes to be in master because now our store is complete in order to do that we need to switch back to the master branch by a-calling git checkout master which now switches us back to the master branch and we can see that and down on the bottom left-hand visual studio code or if you just think good status hit enter it'll tell you exactly what branch you are on so now we need to run the merge command to merge our changes from our store branch into our master branch so we type in get merged which will tell github or tell git to merge whatever branch we've mentioned into the met branch that we are on which is the master branch so if we type git merge and the name of the branch which is store hit enter all the changes from the store branch will be merged into our master branch and as you can see the store dot HTML pages showed up so now if we run git push in order to update our remote repository with our local changes and hit enter you'll see that now when we go to our remote repository and refresh it that the store page is now there this is because the merge command automatically adds and commits the files with a message that says that it has added the store now if we go back what happens if there is a merge can flip them let's say we have this is the store and in our master branch we change this to say this is the store - and if we add that and if we can knit that with just any message and now we switch back to our store branch which we can do it get checkout store now we are back on the store branch and as you can see this is updated to be this is the store since we do not actually have those changes for master on this branch and let's say we change this to B this is the store one save it add those changes and commit them any message will work and now let's switch back to the master branch so now what we want to do is we want to merge those changes from our store branch into our master branch just like before by calling get marriage store but you'll see right here it tells us that there's a conflict and it is saying that inside of the store dot HTML file there's a merge conflict and Visual Studio code will very nicely highlight this for you but get also will add in these less than signs followed by head as well as the greater than signs followed by a store telling us that the head is our current branch so everything between these equal signs and these less than signs is what is on our current branch and everything between the great end of signs and the equivalent signs wishes from the store branch will show up here and it's telling us that git is not smart enough to know which change it wants to add and it is saying that we need to figure out which chain should be saved so all you need to do in order to decide on what change is going to be made is you look at it and say oh okay well we want this is the store - we don't want this as a store one so we delete this is the store one as well as all of these signs that get crates so if we delete that delete these make sure we got it all set up with no extra line breaks in there save it and now we have just decided on what conflict we are going to resolve from we decided that we want to take the changes from the master branch and none of the changes in the store branch now all we need to do is add that file by calling it period after and then we just called git commit this is because it will automatically generated commitment if we hit enter as you can see it opens up in Visual Studio code editor which is something that if you watched my video on studying up git and github was an option that I gave you as opposed to using the terminal once you have gotten in here we see that git has already created a message for us so all we need to do is save this file and close it and it'll automatically create that merge for us and now if we get pushed set it up to our remote repository you'll now see that when we go to our remote repository refresh it and we open up our store that HTML page we have this is store 2 which is exactly what we wanted here now in order to simulate having multiple people working on the same project I'm going to reclone this project copy the URL here I'm just going to clone it onto my desktop so but I can make a clone command followed by that URL hit enter and now it'll clone that onto my desktop and you'll see that if we open this up using visual studio code we have all the changes from before so now let's open up the terminal and imagine that we are now a different developer working on this project and let's say we come in here and we want to add another line in here that says the real end save that what's and with a message that says John's changes because we're going to pretending that we are John and let's push that up to the repository and there we go now if we go over to our repository refresh it you'll see that this test has John changes at the end here but if we go to our original project this one that is open inside of not desktop this is the original one that we created you'll see that when we go to the test John changes are not here this is because we need to use the git pull command to update our local repository from the remote repository all we do type in git hold hit enter and it will pull down all of the change from the remote repository for the branch that you are on and since we are on the master branch it pulled down the changes from the master branch which is where John added his changes that about sums up everything that you need to know about get the whole process goes first create a project on github next clone that project down to your local computer add any amount of code and changes that you want using the git add command and once you finalize those changes into a coherent version use the commit command to commit those changes to get and then lastly after you've done all the adding and all the committing that you need to do for a particular feature or project then you use the git push command to push those changes back up to github you also need to use branches in between there to be able to create new branches for certain features so that you do not bring different features into each other instead of using just one branch the master branch for example where all the features would be you now have separate branches for each of your features so that your code does not get mixed together and then at the end merge those back into master since master is your overall branch that describes the current state of what your project is and that's all there is to it if you liked the video please remember to leave it like and subscribe for more content and if you want to see a more in-depth tutorial involving some of the features of get that I was not able to cover in this video please make sure to comment down below letting me know thanks for watching
Info
Channel: Web Dev Simplified
Views: 194,872
Rating: undefined out of 5
Keywords: WebDevSimplified, learn git, learn github, learn git in 20 minutes, learn git and github, git guide for beginners, github tutorial 2018, git tutorial for beginners, git tutorial 2018, learn git 2018, learn github 2018, learn git fast, learn git in 30 minutes, learn git in one video, learn github in 20 minutes, learn github in 30 minutes, learn github in one video, learn version control in 20 minutes, git simplified, git for beginners, learn git 2019, learn git today, git
Id: IHaTbJPdB-s
Channel Id: undefined
Length: 23min 29sec (1409 seconds)
Published: Thu Jul 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.