Git Tutorial for Complete Beginners | Learn Git in 35 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video i'm going to tell you everything that you need to know to start working with git the contents that we are going to cover in this video are first i'm going to give you an overview of what git is and why you should be using it we'll then see how you can install it and how you can configure it we'll then see how you can convert a local folder or a repository into a git repository i'm then going to talk about the git workflow and then we'll add some files and modify some files to see how git is able to track these changes finally we are going to talk about branches how you can work on different branches how you can switch between branches and how you can merge changes from one branch to another hopefully this video should give you a complete idea of how you can start working with git i'll be making a separate video on github and how you can link your local git repository with the github repository so if you are interested in that then please make sure to subscribe to the channel so you don't miss that upload hopefully that video will come up by next week i'll also make sure to leave the timeline to each of the different sections that are covered in this video in the description below so if you want to skip and watch only a particular section then you can do that okay now without wasting any more time let's get started before we start working with gate let's first understand what git is git is a version control system now what exactly is a version control system it's basically a software that lets you track changes to any files or folders within a git repository now what exactly is a git repository your any local folder or directory can be converted as a git repository and it's very simple to do that i'm going to show you how to do that in a short while let's say you're working on some project and you have written some program in a file and you save that file now let's say over time you would be making some changes to that file so maybe adding new features to the program or you want to fix some bugs etc now every time you make a change you would have to save those changes into this file so you have two options one you can save those changes over that file so what happens is you would kind of lose the older version of that file and let's say over time you end up making a thousand different changes and after a thousand changes you realize that the last 10 changes were incorrect and you wanted to go back to the version of the file which was 10 changes prior to the current file then it is almost impossible to do that because you do not have any backup of that file because all the changes you just saved it on the existing file now the second way of saving changes to that file would be that every time you are making some changes and trying to save into this file you make a backup of the older file so let's say initially you had a file and with the file name like file1.txt and then you make some changes and you save it so you save it like file2.txt and then when you make a next change you save it like file3.txt so on and so forth so if you end up making a thousand different changes over a span of few months you would end up having thousand different files this basically is redundant and basically a thousand different copies of the same file which is actually not required and we are just talking about one file your actual project might be already having hundreds of different files and for each uh one change if you end up making a copy of the whole project it's basically not required and in case in future if you wanted to go back to some previous version it's very difficult to know which particular version of your backup had the change that you are looking for now this is why we need to use a version control system and git is one of the most popular version control system in the world what git is going to do is it's not going to create multiple copies of the same file but every time you make some changes to that file it's going to store a history of that file and in future if you wanted to go back to some particular history or let's say a 100 versions prior to the current version you can pretty easily do that and you can pretty easily do that because you can actually see the difference between an older version of the same file with the current version and that is why it's so important to use version control systems okay now that you get some idea about what a version control system is and why you should be using one let's see how we can start working with git okay so the first thing that we need to do is we need to install git and to install gate just go to your browser and search for git and then go into the git dash scm.com website and here since i am on mac it's already showing me the git version for mac if you are on windows it will show you the windows version just click on the download and then for mac you have a few different options to install gate i think for windows you just have one option so it's just like you just click on the download button and then install it similar to any other software even here it's pretty straightforward so i would recommend to just choose homebrew you can choose any other methods of installation as well but if you already have homebrew then it's pretty straightforward just run this command and it will install git on your machine but if you don't have home riff you can just right click on this and go to the homebrew website and here you have this command so you can just copy this command go to your terminal and then just paste this command and execute it it will just install home view so it's pretty straight forward once the homebrew is installed you can just go ahead and copy this command so let me just show you let me just copy this command and run it in my terminal and this will install git so i have already installed git so it should tell me that uh the get version is already up to date but if you have not installed uh then it will install git so it's telling that it's already installed okay so this basically how you can install gate it's nothing complicated pretty straightforward git is now installed in order to verify that you can just go to your terminal and then just type git dash dash version this will tell you the version of gate that is currently installed on your machine the next thing that we need to do is we need to configure git so it's nothing complicated we just need to configure our username email and default editor which we can use in order to edit some files if we want to so in order to do that we can just type git config dash dash global and then say user dot name and you can pass your name i'll just pass tech tfq similarly i can configure my email id so i'll say email equal to take tfq at gmail.com the last thing that i want to configure is my editor so i can just say git config dash global and then i say code dot editor and then here you can pass any editor you like as your default editor for git i am going to pass code code basically stands for vs code now for this to work you need to first go into your vs code and here you need to search for your command palette and here just search for install code okay so this particular command that says install code command in path just click on that it's going to install code command in path so what this will do is after this if you ever go to any path from your terminal and then just type code it will automatically open the vs code so to see that i can just type code and you can see that it's automatically opening vs code so this basically it we have configured what we wanted to configure for git if you want to see a list of different configurations you can just do git config dash dash list it will show you all the different configurations for your user okay so now we have configured git the next thing that we need to do is we need to create a git repository so let's go into our desktop so i'm in my desktop and if i see i have nothing here so let me create a folder here so let me say mkdir projects and let me enter into projects and here let me create another folder and i'm going to call it like digital clock because this is a project that we are going to create during this video so i create this folder and i enter into this folder okay so one thing to remember is that as you may know whenever you are working on project all the files about a particular project would be within a particular folder right so if you have multiple projects then you would have multiple folders holding uh each folder holding the the files of a particular project same way for gate you need to create one git repository for each project okay so now i am inside my folder digital clock and i want to make this particular folder as my git repository so if i just type ls there is nothing here and if i want to see the hidden files i can just say ls dash a and you can see there is no hidden files as well now in order to convert this particular folder as a git repository i can just run a command called as get init and that's all so this will initialize git in this particular folder so what actually happening is it's not actually going to create any files or folders but there is one hidden folder that it's creating so in order to see that i can just say ls dash yay and you can see that there is a dot git folder it's a hidden folder that has been created now this is required for git in order to track any changes in this folder okay so now we have converted our local folder into a git repository now we have our git repository now let's add add some files and make some changes to see how git will be tracking these changes okay so let me just clear this let me just zoom this before we add any files or make any changes let's first check the status of this repository we can do that by just saying git status you can see that there is nothing to commit meaning that this basically is an empty folder we have not done anything in this folder so basically there is nothing forget to track one important thing to note here is when you type git status it's going to tell you the branch on which you are now it's telling me that i am on branch master by default git will always have master as its branch what exactly is a branch a branch can be said like it's an independent snapshot of your repository and each repository can have multiple branches so let's say you have a branch master so any changes or any commits that i do on this branch can only be seen from this branch it will only impact this particular branch so if i create another branch let's say child and make some changes there add some files and do some changes to a file then those changes can only be viewed from that particular branch so it's something like an independent snapshot of your repository so and your repository can have multiple branches okay so i'm going to tell you how to work on branches in a short while but for now this is just an overview so we have seen the git status and there is nothing much happening on this repository as of now so let's go ahead and create some files so i'm going to create a file by using the touch command i'm going to say file1.txt and i'll create another file say let's say file2.txt if i click ls it's going to show me the two files and now if i do git status you can see that i'm on branch master and then there is these two files under the untracked files category so basically what git is telling is there is something that has happened in this folder there are two files that have been added into this folder but these files are untracked meaning that gate git is currently not tracking them and the reason why git is not tracking them is these files are not yet added into the staging area this is where i'm going to give you an overview of the git workflow so whenever we have a git repository what happens is there are internally three layers that git refers to the first is the working directory which is basically your local folder that you have created and then there is something called as a staging area so in order to commit something that is when i say commit it's basically saving the changes or files to the git repository in order to commit something to your git repository first it needs to go through the staging area that is from our working directory we need to add the changes to the staging area and once the changes are there in the staging area from there we can commit it to the gate repository once the changes are committed it is saved permanently okay whereas once it is in staging area you still have the option of either removing it from the staging area or then committing it permanently to the gate repository okay so this basically the workflow now if i go back to my terminal so i have these two files and these two files are untracked so these are just added in my working directory they are still not there in my staging area now if you want to see what are the files in our staging area we can just run a command like git ls dash files okay currently you can see that there is no files now in order to add these files in our staging area we can just use the command like git add and then the file name so i'll say file1.txt and i can pass different files here so file2.txt now these files are added so now if i check the git status you can see that from red now these files are shown in green and you can see that it's telling changes to be committed meaning that these are now present in the staging area so as i told you in order to commit any changes to git repository there are two stages first we need to add it to the staging area and from there we can commit it to the git repository now if i look at my staging area files so i can just say git ls dash files here you can see that now there are two files added into my staging area once these changes are present in staging area from here i can commit it but before i can commit it you realize that you actually didn't want to add file to.txt to staging area you don't want to commit it so you can actually remove it from staging area and actually git is already recommending the command in order to unstage it so you can just type let's say git rm dash dash cached and then the file name so i'll say file2.txt i don't want to add it so if i just run that and now if i just check git status you can see that it's telling there is one file that is ready to be committed meaning that this is present in my staging area that is file1.txt and then there is another file file2.txt which is untracked meaning that this is only there in my current working directory but it is not present in my staging area okay so this is basically the flow now for now let's say i want to add this file to as well i want to commit this as well so i'll just go ahead and add it so either i can mention file do dot txt here or instead of mentioning the file name i can just mention a dot now what dot will do is everything that is there in your working directory or your local folder will just be added into the staging area okay so now if i check my git status you can see that these two files are now added in my staging area and we can commit it so in order to commit it i can just say git commit minus m that is dash m and then you just pass a commit message this commit message is very important you should always make sure to provide meaningful commit message which can indicate the changes that were done so here i'm just going to say added two new dummy files so my files are now committed now if i just clear this and if i just say get status we can see that there is nothing now the git status it's telling that the working tree is clean so there is nothing to be committed so this is basically how we have created two files and we have added that files into the staging area first and then from staging area we have committed it into git repository now you might ask why do we need to add files to staging area before we can commit it the reason is that before you commit something permanently into the repository you just want to remove it once and make sure that all the changes are correct and as per what you wanted so it's something like in caution you first add it into a staging area and then you review your code changes and once you think everything is fine only then commit it because once you commit it it is permanently saved now there is also a way that we can skip this process of staging area and we can directly commit the changes to the git repository but it is not recommended and i'm not going to cover it in this video so we have seen how we have created two files and added that file staging area and then committed it to git repository now let's say i want to create another file okay so i'll create another file let's call this like digital clock dot py so this is actually going to be my real file where i'm actually going to write a simple python program uh which is going to display a digital clock in my screen but i'm going to work on this project step by step so i'm not going to do everything at once i'm going to do step by step and do several commits so you just get a flow of how we actually use git to record different changes so the first thing that we need to do is let's open this file and write a simple code so in order to open this file i'm going to open it in my default editor that is vs code so i just say code and then i pass the file name that is digital clock dot py so let me open that folder just so that you can see it and so this is my folder and this is my file and this is where i'm going to write my program so i'm just going to write a simple program okay so i have written a simple program it's just going to display some screen i'm going to save it and let's see what what is output from that program so i can just say python 3 and the program name you can see it's a simple screen now this is fine so let me go ahead and commit this file okay so i can just say git add dash and then get commit dash m and then a commit message so i'm going to say like added a new digital clock program okay so i've done the commit and if i check the status it's nothing else on this branch so if i wanted to check the history of all the different commits that i have done on this particular repository i can just type the command like git log and it will show up all the different commits so this was the first commit that we did if you remember this is a message that we gave added two new dummy files and you can see here that this is basically the commit hash id it's something like an unique identifier for each commit and then you have the username and the email that we configured initially and also the time where of this particular change okay so this basically the history of the first commit and then i have another uh commit here which i just did where i added a new digital clock program okay and here you can see that i have something like head head is something like a pointer which will either point to a branch or it can point to a commit so i can change this head to chain to either a particular commit and we are going to look at that in a short while for now uh just imagine head to be something like a pointer so it's pointing to the uh current commit or the current branch what we are working on okay so this is my log i can also print this log in single line so i can just say git log dash dash one line just it will display the whole log message in a single line okay so now i have created a program here like digital clock dot py which is basically showing a sample screen now i want to modify that to maybe make it more colorful and then hopefully add a digital clock over there okay so let's clear this and now let me open that file and make some more changes so i'll open this file and now what i'm going to do is let me add some background and add some label with some welcome message or something okay so i have written this program now let's go to my terminal and try to run that so i say python3 and that program and you can see now this screen has become slightly colorful and it's displaying a welcome message so let me close this and let me now commit these changes so i can just say git add dot and then i say git commit dash m and then a message so i say added a welcome message okay so now let me clear this and just check my log you can see that i have three different commits now all in my master branch now if i execute this program again so you can see it's displaying welcome but somehow i didn't like it and i want to remove this welcome message so what i'm going to do is again i'm going to make some code changes so i can say that code and that program and now here i'm just going to remove this welcome message okay so i'll remove that i'll save it i'll go back here i'll run this it's a blank blue screen this is fine and now i want to commit it so i can just say git add dot and then i say git commit minuscm and then i pass a message saying that removed welcome message okay so if i clear this and if i check my log you can see that i now have four different commit history so this was my first commit my second commit my third commit when i added a welcome message and my fourth commit when i removed my welcome message so all this is fine and i have only worked on my master branch now what i want to do is i want to add a clock a digital clock into that screen but since it's a feature that i'm going to add to the screen i don't want to make all these changes directly in the master branch because master branch is generally the branch which gets committed into production and that is what is basically getting released so we generally do not want to do some experiments or try out new features using the master branch so whenever we want to try out new features or do some experiments we generally do it in a different branch so what we are going to do now is we are going to create a different branch which will basically be a copy of master and then on that new branch i am going to actually make the digital clock and then if i like it only then i will merge it to the master branch okay now to do that i'm going to create a new branch so but before that let me just okay maximize this and go back here so this looks more cleaner and here in order to check all the branches that we have in our repository we can just type git space branch here it's telling that there's only one branch master and this star symbol basically indicates that this is the branch you're on now in order to create a new branch i can just type git branch and some new branch name i'm going to call this like clock okay so now if i type git branch you can see that now i have two different branches and the star is still pointing to master meaning that my current instance is still pointing to the master branch i want to change it to the clock branch but before that just let's just look at the log messages so let's see in which branch i am on so i can just do that by saying git status and you can see that i'm on branch master here let me check the log git log and i just want in one line so i'll say this and you can see that in master i have four different commits and the last command that is which is the head is pointing to my master and clock the different brand that i created is also in the same uh comment okay so meaning that master and clock are currently having the same code their versions are the same now let's switch back to clock how you can switch from one branch to another is by using the command called as switch so i can just say git switch clock and now you can see that it's telling switch to branch clock and let me run the log message here and now you can see that the git log in my branch clock here the head is pointing to branch clock but both these branches master and clock are pointing to the same commit meaning that all the files and codes are exactly the same across these two branches but now we are going to do some additional changes in the clock branch let's clear this and do some changes in our code to actually print the digital clock so i can just say clock code digital clock and here now i'm going to add a function to display the digital clock okay so i have added my function which should hopefully print the digital clock on my screen if i go back and run that program i can just say python 3 and the function name so you can see that inside my blank screen now a digital clock is being printed so let me close this and let me now commit this okay so i'll just say git add dot and then i say git commit and i pass a message saying that added digital clock to the screen okay so now i have done the commit let me clear this and let me now look at the log so i say git log dash dash one line and you can see that now from this log you can see that my master is in this particular commit but my clock since i made a new change and i added the digital clock it's having a additional commit that is this one okay so now you can see that my master branch and my clock branch are in two different versions okay now to verify that what we can do is now we are in the clock branch right so i can just confirm that by saying its status yeah so we are on clock if i run this program that is this program you can see that the screen is printing the digital clock but now if i switch back to the master branch so let's say git switch to master and then if i run that program you can see that there is no digital clock it's just a blank screen and that is because i am on the branch master so meaning that my master branch is having this code you can see here i have this particular code where i do not have the clock function but if i switch back to clock and if i go to go back to vs code you can now see that it's having this particular function so it's the same folder same file but two different versions of the file the reason for that is because of two different branches my master branch has a different version of the code and my clock branch has a different version of the code and this very useful so you can maintain your master project or master software or the main software as it is when you are trying to experiment or create new features you can just create those features in a separate branch and then if you're satisfied only then you can merge those changes into the master branch now let's try to do that so let me clear this and let's switch back to master so let's say switch to master and i'm switch to master now i want to merge all the changes that i did in my clock branch into my master branch okay so but before that just let's just check the log here i'll just say one line and you can see that i only have four commits and the last comment is master where i said remote welcome message so basically the addition of the clock is not present in master so i'm just going to merge the clock branch everything from clock branch into my master branch so how i can do that is i can just use the command like git merge and the branch name clock so you need to be on the master branch and then you need to type this command saying that git merge clock so it will merge all the code or all the files or all the changes from this particular branch into the master branch okay so now if i execute you can see that it's done it's telling that it's inserted seven plus lines and it's modified one file okay so now to confirm that what we are going to do is we are in our master branch so let me run that python program and you can see that now my master branch is also having this digital clock okay so if i go back to my vs code you can see that this is my master branch and the clock function has been added just by a merge so let's just look at the logs between these two branches so i am on master branch and let me just check the log git log dash dash one line and this is my master branch and now let's switch back to the clock branch get switch clock and here if i check the log okay so you can see that now everything is same only thing is in my clock branch head is pointing to the clock branch and in master bench head is pointing to the master branch but the commits are all the same you can also verify the commit ids the hash ids they are also exactly the same so this is basically how we have merge changes from a different branch into a master branch now let's say my manager comes to me and tells me that he didn't want the digital clock to be committed to the master branch as of now he wants the master band to still point to a blank screen basically now we need to change the repository to point from this command to actually this commit and this is the advantage of git if you wanted to go back in time and change all the code and changes of your repository as per some old history you can pretty easily do that so if i wanted to switch back my master from this particular command to this particular command i can just copy this commit that is this commit hash id and here i can just say git reset dash dash hard and then enter the commit so now you can see that head is now at this now if i just check my log message here you can see that previously my master branch was having one two three five commits but now you can see that my master branch is only having two commits okay because i changed the master from this particular version to some prior version and that is advantage of using a version control system so now let's say you have a requirement where your manager wants you to now display a red screen with a welcome message what you can just do is if you go to master you now see that it's a blank screen there is basically no welcome message nothing but if i switch back to my clock branch and if i just check the log let me just clear this log dash dash one line you can see that i have lot of versions of my code the last version is the digital clock i don't want that but i have prior to that i have a version here where i said added a welcome message okay but this was not in red color it was in some other color screen right so the requirement now is that we need to add a red screen with a welcome message so instead of doing everything from scratch or creating a new branch and then removing all these changes what i can do is i can use the checkout command to point at exactly this commit and then from here i can take over the code and do whatever changes i want so how i can do that is i can just say git checkout and then i pass the commit id so i want this one so i'll just say this and now you can see that it's telling switching to this commit and you are now in detached head now this detached head is basically uh the reason for that is if i just check my log let me just show you the log just so that you it becomes very clear get log dash dash one line and you can see here that my head is pointing to a commit previously we were seeing that the head was pointing to a branch so the head can either point to a branch or it can point to a commit if the head is pointing to a commit then it is called as it's in the detached head state okay now in the detached head state it's basically not a branch it's not in your master branch it's not in your clock branch it's something like its own uh temporary branch and from here you can make code changes and then commit it and then maybe create your own branch for those changes okay so let's see how to do that so let me just clear this and you can see that i am here dash dash one line and i have this some welcome message let's see how my python program looks now so i say python 3 and run this it's displaying welcome this is fine but i want a red screen so what i'm going to do is i'm going to do some modifications here so i say code and i run this open this program and i have configured the background to be still blue let me make it red and even here let me make it red i'll save this and now let me run my program and you can see now it's displaying a red screen with the welcome message and this is exactly what we wanted now this is not part of any branch it's not part of the clock branch it's not part of the master branch it's just a detached head state so what we need to do is we need to commit this so let's first commit it so let's say git add dot and then i say git commit minus cm and then pass a message change to red screen okay so if i clear this and if i check the log and now you can see that i i started this my init previously my head was pointing to this commit because this is where i did the checkout but now since i made a new change and added i committed it i am getting a new version of this code okay but this is not stored to any branch so what we are going to do is now we are going to change save these changes into a new branch how we can do that is i can just say git switch dash c and then pass a new branch name so i'm going to call this like red screen okay so you can see that switch to a new branch red screen so now if i do git branch you can see that i have three branches and is a new branch that is created red screen let's clear this let's just see how our three different branches are working together okay so let's say grit branch i have all these three branches and now let's say i do get status i'm on my red screen branch from here let's say i want to run my python program so i say python 3 digital you can see it's displaying the red screen with the welcome message now if i go back to my master branch so i say get switch master and from here if i run my python program you can see that the master is just having some blank screen nothing here and now if i switch to my clock branch so if i click to clock and here if i run my python program you can see that it's having a digital clock so basically we are working on the same repository but it's having three different branches and each branch is is having its own version of the code and if you want we can merge between these versions of the code into a single branch so this basically how we work with a version control system in this case git hopefully it was all clear and it was not too confusing if you like this video please make sure to like this video and also add a comment with your feedback and also if you have any questions do ask it in in the comment section below and do subscribe to the channel and i'll be posting a video on github in the next week so make sure you're subscribed and stay tuned for more interesting and useful videos thank you and have a good one bye
Info
Channel: techTFQ
Views: 3,894
Rating: undefined out of 5
Keywords: git, learn git, git tutorial, git tutorial for beginners, git tutorial for complete beginners, learn git in 35 mins, learn git in 35 minutes, learn git in 30 mins, learn git in 30 minutes, what is git, how to install git, git repository, configure git, git workflow, git branch, git checkout, git merge, git switch, git add, git commit, GIT, Git, git for beginners, git basics, how to use git, git training, git course, learn git fast
Id: kY5HtrkjSj0
Channel Id: undefined
Length: 36min 38sec (2198 seconds)
Published: Fri Jul 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.