Git Tutorial for Absolute Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
git and Version Control Basics can be so intimidating and confusing for beginners let's remove all of those obstacles today and review what you need to know about using git that will help you as you learn how to code [Music] hello and welcome I'm Dave today we'll review using git as a beginner and how it can help you as you learn how to code and I'll provide links to all resources in the description below I'll also provide a link for you to join my Discord server where you can discuss web development with other students and you can ask questions that I can answer and receive help from other viewers too I look forward to seeing you there I'm on the web page for git as we get started today and that is at get Dash scm.com let's look at the definition here it says git is a free and open source distributed Version Control System designed to handle everything from small to very large projects with speed and efficiency but what does that even mean as a beginner that can be very confusing well let's start with Version Control and I'm going to look at another website that explains this well says what is version control and it is the practice of tracking and managing changes to software code so so that is Version Control and note it also says it is also known as Source control so you may hear those two terms interchangeably now if we go back to the get page git is a version control system that we have just defined what version control is that's a tool to help you track and Implement Version Control changes to those files and one more beginner confusion that I want to alleviate right away is git is not the same as GitHub and here I'm on an mdn page and note it says GitHub is a site that provides hosting for your repositories and several tools for working with them now it's talking about code repositories that you create with Git but GitHub is a website where you store your code essentially you could think about that as being in the cloud you send your code in a repository to GitHub so that is totally separate so let's talk about what I'm covering today as we go back to the get home page really there's far too much to learn about git to cover everything in one video and if I could it would be too much for anyone to digest all at once in fact most all working developers still look up git commands in the documentation if they have a need that gets away from the few commands and workflows that they use most often so with that in mind I've made a free get cheat sheet for today's lesson that you can download from a link in the description and it's likely that even after you work through today's video you will want to refer back to this cheat sheet as you continue to get familiar with Git now today I want to cover setting up git on your computer and the things that you as a beginner will do most often with Git while you're learning to code now the first step is to download and install git and you can see it has identified on the web page that I'm using Windows so it already provides the download for Windows but there is a downloads link that you could go to and git is available for mac and Linux as well go ahead and download the version that you need and install that then come back to this video Now quickly I also want to highlight that I'll be using visual studio code today as my code editor and that's where I will interact with Git in the terminal window so if you don't have Visual Studio code you'll want to go to code.visualstudio.com as you see up here and go ahead and once again download the version that you need it's available for Windows it's available for mac and Linux get the version you need and install that if you don't already have Visual Studio code I now have vs code open and I have a empty project folder or projects actually with an S here and so there's really nothing in it however you need to know how to create a folder or you could also call it a directory both of them mean the same thing on your computer so I'm not assuming a lot of computer knowledge here but you do need to know how to create your own directory or folder on a computer then you can go to the file menu and choose open folder and browse to that folder to go ahead and open it up in vs code once you've done that we need to open up a terminal window inside of vs code we can do that by going to the view menu and choosing terminal but you also see there is a shortcut which is control in the back tick so that's what I'm going to do is press control and the back tick now that may be a little different on Mac or Linux but that's what I have on Windows and you can see my terminal menu or my terminal window actually showed up and it has menus in it now I'm going to drag this up because we can make it a little larger and then you see I already have bash here now that's my default and that comes from git so I'm going to click on this Arrow over here by the plus symbol and it will show us some different options you can see git bash is my default but I also have a command prompt and a Powershell things that Windows provides and you can set the default profile so you may not have git bash as your default already you can go to select default profile and then you can see the different choices here and I'll just choose get bash and that's what I already had essential actually but we could also go here and if you really want to get into the settings of vs code I believe you can do that as well with configure terminal settings and then instead of what it gives you by default just press backspace to get rid of that search for terminal and then if you're in Windows you could search for default Windows and I guess I need to pull this back down so we can see the settings as much and I'll scroll there we can see terminal integrated integrated pardon me default profile windows and I have get bash set so as you might guess you could also set a default terminal for mac and I don't have that set I'm on Windows almost all of the time and then there's also one for Linux that you could set as well in your profiles because different developers possibly use more than one type of computer and they can pull in their vs code profile so just so you know those different settings are available but all you really need to do to get started is of course choose get bash as your default as I've already shown you how to do now that brings up the next thing you can see I now have two terminal windows open it shows them here on the right so I can switch back and forth between the two I'm just going to click the trash can icon to get rid of one because we only need one now you've already installed git as well so since you have done that we should be able to type git and then dash dash version and we can confirm it's installed you see I have version 2.39.1.windows.1 it says and instead of dash dash version we could also just type git Dash V and we should get the same result that's just a shortcut four dash dash version and now not a git command but just a terminal command we can type clear and it will clear everything out of our terminal window now that we've done that and now that you've installed Git it is time to configure git so we can type get config and and then we'll want to make these settings Global not just for the one repository however we can do that when I say repository I talk about the code that's going to be inside of the folder that we are in now you can do local settings for just one but we're going to make this Global so Dash Dash Global and then we're going to set a user.name and then I would say my name is Dave gray and press enter now I already have this set it wouldn't hurt if I change it but that's exactly what you need to do there for years now after you do that instead of git config dash dash globaluser.name we're going to use all of the beginning of that and then it's going to be user.email and then you would just want to put your email at your email host.com essentially and then enter that as well now git is going to save those in the config file you have to keep those on file for git now that we've done both of those we can just check the values and by doing that let's look at just one of them I'll say git config user.name and I'm really not setting anything so I'm just checking the value and I press enter and there we can see the username that I have inside of my git config file now there's one more configuration that I do recommend although it's not required however back in 2020 I believe they started renaming Master branches main instead of Master so you can just set that globally so it always happens for you so type git config and we'll learn more about what a master branch and a main branch is but right now we just want to go ahead and set this and we'll once again put Global and then I'm going to type init Dot default Branch so when we initialize git it's going to name our default branch and we'll put main here and then just press enter and now that should be remembered every time we initialize git now I'm going to press or I'm going to actually enter clear one more time to clear that out and then I just want to say that if there's any command you need to know about as we go through this or as you learn more about git in the future you can always type git help and then whatever you need to know about so I could say get help config if I could spell config press enter now it's going to launch the browser here so I need to bring the browser back up and it launches a page and here is the help page for git config so just so you know that is available to you before we start working with Git there's one more setting I would like to change in Visual Studio code so let's go ahead and close the terminal window temporarily now we need to open the settings to find those in vs code we go to file and then you can scroll down to preferences and then you can scroll up here or move the mouse up to settings but notice there's a shortcut Control Plus the comma so instead of using that I'm going to get out of the menu and just press control plus comma Now again that may be different in Mac or Linux but the file menu should show you what it is now inside of settings I'm going to search for exclude and we shouldn't have to scroll far down just look for the files exclude because we want to see the get folder in the file tree I prefer to see that and we sometimes we have that hidden or maybe a default in Visual Studio code so when you see this under files exclude with the dot get I would recommend just deleting that so you can always see your git folder there now if we come back up it moved that for some reason but I'll find that where we were files exclude it's no longer in there and of course you can leave leave the rest of these the way they are but we want to go ahead and be able to see our DOT get folder once we initialize a repository or download a repository that has a git folder and now we're ready to cover what should be the most common use of git for beginners that are learning how to code and just learning about git in general and that is to download tutorial code or lesson code from someone like me that has provided it along with the tutorial and I usually do that in the description of my videos I give a link to resources and a lot of times there's a link to GitHub where I have the code for the tutorial that you're watching so what you want to do is go to GitHub and I am at my HTML course in GitHub now I could provide a link to this in the description but what I plan to do is provide a link to a code playground where you can just kind of download that repository and then use it to practice with Git as well but I'm going to use my my HTML course for this example now that I'm on the page here in GitHub for the HTML course and then you go to the code button and you can do this in any repository that you find on GitHub and from there we can just copy this URL right here for this repository so I'm going to click the copy right here and now that it says copied I'm going to go back to visual studio code and we're going to download this repository so we can work with it inside of Visual Studio code so to do this I'm once again going to open a terminal window do you remember the shortcut to do that it's going to be control and the back tick and then that opens that terminal window back up from here we're going to clone the repository so I type git clone then all I have to do is right click I'm on Windows and I know that's a little different for mac and Linux but in whatever you would do to paste in that URL that we copied and now we'll be able to clone this and it will come down in one large folder and all of the contents will be within so I'm just going to press enter and it's cloning the course and you can see that happen fast and now we have this HTML course directory right here and then inside of this we can see everything now notice you can see the dot get folder because we removed that from the settings where it would have been hidden otherwise and I do like to see that in the file directory now I want to point out that we are still in the Parent Directory which is projects and then inside of projects is the HTML course directory and we can see that reflected in our terminal window here at the bottom as it shows where we are and the last directory listed is projects over here so we have a couple of options we can go to the file menu and choose open folder and then we could browse to that HTML course and that's probably what I will end up doing so we only see it over here and it will be where you see the word projects however if we were just going to do something in the terminal you can also use the change directory command which is C D and then we could say we want to go into the HTML course directory and now you can see this changed here and we have projects slash HTML course so our terminal can now do things inside of that if we wanted to but for now I'm just going to hit the trash can close out that terminal and then I'm going to go to Open folder here which you can see there's a shortcut if you wanted to press Ctrl K and then Ctrl o we'd open a folder I'm going to go into the projects and then we have HTML course I'll select that folder now you can see the change here I'll close that but we have the HTML course here at the top and here's all of the directories that are inside of that git repository that we've created now there is something here I don't really need any contributions back to my HTML course I'm going to leave that as is with the video but you have downloaded the code to practice and work with it and that's great but but you don't really need to contribute or we're not working on a project together so when you get tutorial code you don't really need this get directory anymore so we have already cloned it and downloaded it you won't be sending any code back to the repository so that being the case let's open a terminal window and now let's learn how to remove the git repository so essentially you wouldn't be using git with this repository after you've got the tutorial code unless you wanted to create a new repository and save your changes for a new project or just something that you're tracking you wouldn't be sharing that back with me so to do that we can type remove which is just RM now that's not necessarily just a git command that's another command line code just like CD was that I showed you but after that we can type Dash RF and those are two flags for recursive and force you don't need to know the details so much but it will absolutely make sure that this directory is going on after that I'll just type dot get and then press enter and you can see that git directory has now been removed from our file tree and really that's the basic workflow that you need as a beginner you want to download tutorial code that I've provided quickly and easily and you can do that with Git clone after you have set up git on your computer now let's move on to the workflow you would use if you were creating your own project I have created a basic HTML project here and we can see the different file types in the file tree and index.html a main.js file a readme.md which is markdown and style.css and that is the full project so let's consider this our project that we want to use git with and so once again we'll open up a terminal window with control and the backtick and from there I'm going to type get init which means initialize this code project into a repository for so I'll go ahead press enter and now we can see that is exactly what happened and notice here in vs code some changes happened as well now all of our file names in the file tree are green and they all have a u next to them as well so we need to see what this really means I'm going to drag this up just a little higher so we can see more of the terminal output I'll also type clear to clear out what's already in there and then let's type git status and this will tell you exactly what status your repository is in so I press enter and we can see several things and it put out just enough information that it scrolled up so it started out with telling us we're on the main branch which we haven't learned about creating other branches yet and as a beginner you're not so much worried about that you've got the tutorial code or your own project that you're just starting to work with so we'll get into that some other time but right now we're just on the main branch and there's no commits yet but we have untracked files and that's what the u means here next to each of these files in our file tree the status of these files is currently untracked and it shows us all of these and it also tells us how we can add these files to the repository and what will be committed and that says use the command get add and the file name so it says nothing added to commit but untracked files are present so we could add those into visually using git add and the file name such as git add and then index.html if we wanted to do that however when I have this many files and I'm ready to add them all at once I can just say git add and then type a period which means all then I can add all of these at once so I'm not going to do that yet but I'm just going to show you what I will do when we add those files what I want to do first is create one more file over here and let's call this notes dot txt which is a text file so let's just say I had a file with some notes in it and I save that right now it's also untracked so by the way instead of typing git status as we previously did and we see the results above of course we can just type git status s to get a shorter version of that so let's go ahead and do that and now you can see we just have question marks here double question marks by all of the files that are untracked so that's also a possibility when you want a shorter output right now I'll go ahead and do just get status one more time we'll see this full output and it says all of these are untracked and can be added but I don't want to add the notes.txt file So to avoid that I'm going to create one more new file and it's a special type it's called git ignore but it starts with a dot just like our git directory and then I type git ignore all lower case and now I can list files in here that I do not want to include I could also use patterns such such as an asterisk Dot txt and then it wouldn't include any text files whatsoever but if I just wanted to exclude the notes.txt I could put it in here exactly like that now when I save this file with Ctrl s you'll see that grayed out it's no longer green it's no longer untracked it's just totally being ignored by git so that won't be added when I add these other files so now let's go back I'll close those and here in the terminal I'm going to type git add and the period and now they should all be added and yes it changed from a u over here to an A so all of these files are now added including the dot get ignore because you want that included so these files can have different statuses so we've already talked about the untracked status and now after they have been added they are staged for a commit which is the other status you would call this you would call it staged however now a file could also be modified so let's change something I'll drag this back down and we would get another status let me just move the exclamation mark here and then Ctrl s to save and we should see something change over here in the file tree we see index.html M so we know that has been modified now so that change is not staged the previous version before we removed the exclamation mark is so now let's check the status and see what it has to say to us in the window and we can see we still have this new file here index.html staged to be committed however we also have a modified version down here index.html and that's where we removed that exclamation mark so it can be in both actually because one version is staged to be committed and then the newer modified version is not now notice if I wanted to ignore or discard as it says here discard those changes in the working directory I could use this command it is telling me get restore so if I typed git restore and then index.html enter that this switches from modified back to added because it restored the previous version and now notice our exclamation mark is back so this version control is tracking the different versions of our changes and that also allowed us to revert to a previous change if we needed to or I said previous change I should say previous version it allowed us to go back from what we had changed to the previous version so let's once again drag this window up so we have a little more room check our status one more time and you can see all of these new files are staged and ready so let's go ahead and handle that now and oh it's giving us another message by the way we could remove the file to unstage if we wanted to if we had them at this point and we wanted to so git will help you with the commands you need but what I want to do is go ahead and make a commit so this needs a message to go along with it so if I type git commit I typically would want to type Dash M right here to say I'm going to go ahead and include a message with this commit and if you don't do this it's going to open up a text editor and ask you to do it and of course it's going to wait for you to save that file so first let me do a Commit This Way which is what I would typically do and I'll just say first commit and press enter now all of those changes will be committed and notice in the file tree no longer green no longer orange they are white at least with my current theme and it doesn't have any letter next to it all of those changes have been committed so now let's check the get status once again and it says there's nothing to commit our working tree is clean but let's say we decided to go ahead and remove that exclamation mark once again so let me remove it from the index and now we'll see that that file has been modified now instead of having a commit or an ad and then a commit where I would have to type get add and index.html and then git commit Dash M and give a message I can kind of do all of that at once if I want to so I can just type git commit and then Dash a for add Dash M for message and then type my message like added exclamation mark and let's go ahead and press enter and now it does all of that at once and so it added the file and committed it so you can do that shortcut with the dash a and dash M on git commit if you want to let me make one more change and I'll just change something here in let's add another console log statement to the JavaScript and you may not know JavaScript yet and that is okay too we just want to say hello here inside of the console we don't we're not doing much here with the JavaScript it's just there to make some changes and play around with Git this is our get playground so now we have a main.js file that has been modified and we want to add and commit that but let's say I'm going to just do it individually or I can do git commit and I could do the dash a for the ad but let's say I leave off the dash M for the message and just press enter and now it says hint waiting for your Editor to close the file notice it opened this text file up here and so you have to have a commit message and if you forget to do the dash m flag for message and type A commit message it's going to open up a file like this where it is going to wait for you to type the message and save the file before it goes ahead and completes the commit down here in the terminal so we can just say something here like added additional console.log statement now Ctrl s to save this file and I've saved it and now it says waiting for the editor to close a file so even after we've saved it we still need to close the file and once we have actually put in a message it will confirm that and it will complete the commit so now you see the confirmed message here it's much easier to just add the dash m flag and put the simple message here now when you make messages like that you're working on a team you're going to want to be specific and they could possibly have rules you're following but add some detail to your commit messages and there are complete tutorials just dedicated to making good commit messages when you're working with Git so that's a whole other consideration now also notice in the file tree when we had to add that commit message it opened up this file so it made this drop open and we can see all of that I don't want that to confuse you as a beginner just know that there's a lot of stuff inside of this get directory which I do like to show but you do not want to go in here and change things on your own unless it asks you to like that commit message so pretty much just leave this directory closed if you're showing it in the file tree and now with our simple project ready we're going to go ahead and send it to GitHub so we need to learn a little bit more about how to use git to send our code into the cloud and store that repository in GitHub so after you have added and committed your changes to your repository you're now ready to send it to the cloud which usually means GitHub but there are other Alternatives like gitlab and bitbucket and several others as well but you did see that I use GitHub for the code that I provide with my tutorials so that's where we are so I'm at github.com and you can click sign up here in the top right or if you scroll down at least with their current design you can sign up right here as well got kind of a cool page right now if you go to the sign up page also no matter which one you use just go ahead and sign up and then come back to this video now once you've signed up for GitHub and logged in your first page should look something like this there's a feed in the middle which I'm not paying much attention to but to the left there is a list of repositories you may not have any yet but there should be a new button we can create a new repository on GitHub by clicking new or we can come over to this plus button near the top right and choose new repository there as well either one will work and after that it will take you to a page where you need to name your repository so I'll just call this get code dash playground and this is what I will link to in the description of the video so you can clone this repository and just play around with it as you wish so from there we can leave this as public which may be the only option if you have a free account I have a pro account so I can also make repositories private after that we don't want to add a readme or really do anything else here in GitHub we just want to create the repository and then it's going to give us a page with some instructions so it tells us this is our project get Dash code dash playground it's public and you can scroll down and see the different things they recommend so you can create a new repository on your computer just like this or we already have an existing repository that we were working with so we can use these directions right here and they have a little copy Button as well so I'm just going to click that to copy the code and now we'll go back to vs code and Link our repository that we created on our computer to this new repository on GitHub okay I am back in vs code and I'm going to press control and the backtick to open a terminal window again I'll type clear to clear everything out and now I'm going to just go ahead and click this up Arrow to expand this all the way up because I want to see everything we do now in Windows I can just right click and it's going to automatically paste in those three lines and it's going to go ahead and enter the first two so it will happen fast I'll right click and you can see the first was git remote add origin and then it provided the URL for that GitHub repository that we just created now the next one said get Branch Dash M Main and that's just making sure we're using main instead of Master now remember we already set that default configuration earlier but now we can use this final command which pushes our code from our computer to GitHub and so we set the remote up here and GitHub is our remote and it's that repository we created git Dash code dash playground that is the remote so we're saying git push and then we have the U flag here and then origin Main and so we don't really need to worry about all the different details here this is just what GitHub gave us and they're helping us out so we can press enter and it's going to push this code that we have and now it should all be at GitHub so if I go back to GitHub in the browser and I scroll up and click on get code playground we should now see all of those files we had here and look here we see those messages those commit messages so we had first commit on these three files that was the last time they were committed but then we had another commit that said added exclamation mark and we had another commit that said added additional console log statement it tracks each one of those changes that is our Version Control and then you can see the readme.md file the contents of it are shown here on the main page of your GitHub repo and when you hear repo that means repository but a lot of people say repo for short and so I just had the words get playground inside of this markdown file and now in GitHub if we want to see anything about these files inside of any of these files we can just click a file and here you can see what I had inside of the readme but I can go to the HTML and we can see all of the code that was in here as well we can even click raw and it will give us just the raw code here so you have several options for viewing the code in GitHub and remember you can go get this from my GitHub now and you can copy this and you can clone it and download the code to your computer just following those directions that I provided earlier in this video now you may have ran into one thing that I did not highlight here because I don't have to do it I already have mine set up and that is GitHub may have asked you for your username and password as you push that code notice here I just said get push and it just happened but that's going to happen however you can cache those credentials so I can quickly go over what you would want to do to Cache your credentials and not have to enter them in every time you pushed code to GitHub and you might do that fairly frequently so what you would want to start out with is the command git config Prudential dot Helper and then the word cash you would press enter it's going to Cache your credentials or a certain amount of time and that default time isn't that long I believe it's like 900 seconds which I think is 15 minutes if I remember correctly but we can extend that so instead of just this command you could type everything that I said there and then in quotes here you could have cash then dash dash timeout equals and you can set this to 36 000. now 36 000 would be one hour so 36 000 is how many seconds it actually is and so the default I believe you said was 900 so 36 000 would be one hour you could make it two hours if you did 72 000 for example now one other thing you might want to add to this as an example would be the global flag so if I come back here after git config we could say dash dash Global now this would apply to any repository you started working with within the two hours let's say or one hour whatever you set that time out to so you could say git config dash dash global credential dot Helper and then you could have cash then dash dash timeout equals and let's say you want to work for two hours and you don't want to re-enter those credentials during that time so this would be the command to enter for that example now it's going to ask you for the credentials once but after that it would remember them for the two hours if you used 72 000 here as the value so really that's a good start for beginners and it's a lot of info so remember to download my get cheat sheet and refer back to it as you play around with what you've learned today and practice is really the only way to get better at using git so create new files modify existing files do all of the things with it go back over everything I've covered today and now that I've covered cloning repositories and creating your own repositories there's still a few more get topics that I think I should cover in future videos things like branching stashing merging and even a team workflow because a lot of get videos you watch will be about working in teams because that's what people do with Git when they have a job however as you're just learning the code I think the things I've covered today day would be the things that you do first so let me know in the comments what you would like to see and I can guarantee I will cover more get topics in the future remember to keep striving for Progress over Perfection and a little progress every day will go a very long way please give this video a like if it's helped you and thank you for watching and subscribing you're helping my channel grow have a great day and let's write more code together very soon
Info
Channel: Dave Gray
Views: 60,951
Rating: undefined out of 5
Keywords: git tutorial for absolute beginners, git tutorial for absolute begginers, git tutorial for beginners, git tutorial for begginers, Git and GitHub, git, github, git tutorial, git for beginners, git beginners tutorial, git github, git vs github, version control, git commit, git explained, how to learn git, git lesson, git lesson for beginners, how to use git, what is git, what is version control, what is github, beginners git, git for absolute beginners, git init, git add
Id: CvUiKWv2-C0
Channel Id: undefined
Length: 36min 21sec (2181 seconds)
Published: Tue Feb 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.