Transcript for:
Complete Git and GitHub Tutorial Notes

Hey everyone, welcome back to the channel and in this video we're going to be looking into the complete Git and GitHub tutorial. So if you don't know what Git is, what GitHub is, do not worry. In this session we're going to cover it from scratch and also everything you need to know basically in order to get started and even some of the most not-so-beginner concepts. So yeah, let's get started.

So what is Git and what is GitHub? Imagine that you have an application or a project folder, right? And you want to collaborate with other people on that project.

Or imagine that you have your application and you now uploaded some new code in your application. You added a new feature, for example. And now your application breaks. Let's say your application is not running anymore like it was before.

And you're like, hey, I would really love it if I could go back in the past you know in that code base in which my application was running i wish there was some sort of a way to you know click a picture of that code base and whenever i want the picture like that exact code base as it was during that time in the past i could have that so basically saving the history of your project like on let's say first july your project looked like this second july your project looked like this third july your project looked like this like some additional files code changes or whatever you want to go back to how your project looked on first july you can do that you want to go back to how your project looked on second july you can do that that's an that's a use case that you can think of another one can be you are contributing to open source for example or you and your friends are contributing to one project now imagine that a hundred people are contributing to just one folder that is what happens in like open source projects as well there's one main folder and so many people are from around the world contribute to that project now you might be wondering how do these people share their code if you want to share a code in some you know open source project you want to add your own functionality or share some changes you made how can you do that we can do that via git and github so git and github allows us to maintain this history of the project at what particular point of time which person made which change where in the project git helps us in doing that What is GitHub? GitHub is a platform, an online website that allows us to host our Git repositories. Now you might be wondering what is a repository? We'll look more into that shortly. Repository is basically just a folder where all the changes are saved.

Do not worry if you don't know about it, we'll cover it in detail shortly. You can think of it as somewhat like this. So you have the email service, right? So you use email and you have the email technology behind it and you know there's like the server and everything happening in behind the back end so that you can relate to with git now what are so many platforms that are providing you sort of like an interface to use this service to send emails to people around the world to have like the nice you know gui where you can see all your emails and everything gmail outlook yahoo mail so on and so forth that you can relate with github or other platforms that allow you to do the same thing like bitbucket GitLab for example. So Git is like the version control system like the tech and there are so many other online platforms that allow us to host these folders or repositories or our projects online so that other people from around the world can share, look, contribute and these are known as like GitHub, Bitbucket and GitLab.

We will be using GitHub because it's one of the most popular ones and it has a lot of nice cool features. so that's the whole idea of why we are using git and github do not worry if you don't understand how we are using it because that is going to be covered just for now understand why we are using it maintaining the history of the project which person made which change in which project when and so on and so forth like also sharing your code on github sharing it with people around the world so if i look into any project on github.com okay let me look into any let's say gina ai or whatever i can see 5900 so you can see don't worry about what these things represent we'll cover into it later on these are based this is basically the history of the project that i was talking about history of the project as you can see so basically uh this person made these changes in the code base red means that this line was deleted green means that this line was added so this person nine days ago made these changes in this file. So you can see the history of the project. And this goes on and on. You can even see the older history.

On 16th July, these changes were made by someone. on 12th July, these changes were made by a lot of people so that's basically the idea and that is what we are trying to achieve here now let's look into how we do it cool so first thing you need is you need to download git so you can go to gitscm.com and here you can download it so you can download either for macOS for Windows or Linux I would not recommend downloading the GUI clients because we will be heavily focusing on the command line like this terminal thing so do not worry about the gui i would recommend you to download it the command line okay so you can download this and it's pretty easy to set up and uh yeah once you are set up you can just write git over here and you can see if you get a list like this it means that git is currently installed okay before we get started with the tutorial now the hands-on part so what is this structure over here on the left hand side you can see my terminal normal command line shell you can use cmd windows terminal whatever whatever you like on the right hand side we have a folder okay so my terminal is currently in my desktop folder and this is just very basic commands and um what basically terminal allows us to do if you're a very you know you don't know much about it it allows us to manipulate the file structure using commands okay that's basically what it's trying to do so here you can see this is the desktop folder currently it's empty and here if i do list command basically list all the things that are in my desktop folder empty this does not show me anything imagine let's say i want a project we mentioned that we need a project in order to maintain the history of that project and do all sorts of things with it that we just mentioned share about it on github you know share it with other people let other people contribute to it as well you want to do that then how do we do that project so you can either you know click right click new folder or there's a command for that mkdir make directory project so you can see i made some change over here using a command and it was visible in my folder project folder is being created currently it's empty i can change directory into it now what do you mean by change directory kunal change directory means something like this double clicking on it now i'm inside this folder how do i double click in the command line and go inside the folder If I do list, you can see desktop should contain project. That is true because desktop contains the project folder and here also I am seeing it. This is just a list command. I can also go inside this folder.

Change directory project. Now I am inside the project folder. This is the view round. If I do list again, I can't see anything because this is empty. So now we have our project.

Let's say now we want to maintain the history of this project using git. Where is this entire history being stored? I create a new file that will be in the history.

I make some changes in the file that will be in the history. I delete the file that will be in the history. So all of these histories are basically stored in another folder that Git provides us.

This is known as a Git repository and it's named.git. That is the name of the folder.. in like Linux systems and like macOS and stuff. These are files that are hidden.

and how do we get this dot git folder how do we get this folder where all the history is being saved just write the command git init initialize an empty git repository repository means a folder okay so you can see it says initialized empty git repository in my project folder now you might be asking hey kunal i can't see anything over here how are you so sure that there's a git folder over here as i mentioned before you can do list you will not be able to see it right now it shows empty but it is there you can do an a tag a basically means show me all the hidden files any file that starts with a dot is hidden in mac os and linux so you can see dot git is here you can also see what is inside this dot git folder i can say ls list what is inside the dot git folder it contains something called head something called objects something called hooks refs config description whatever don't worry about what these things represent right now we'll be going through it shortly okay and don't worry about what this master is or whatever so this is what we have we have a git folder over here now we will be able to maintain the history of the project any change made in this project now git will pick it up git will be like hey kunal you have made a change in this project please make sure you click a photo of it so that other people are aware that you made this change let's make a change let's say Currently, let's say I create a new file. To create a new file, another Linux command is called touch. I can create touch names.txt. so names.txt file is being created over here as you can see has there been a change in my project does anyone in the world know that kunal created a names.txt file is this history maintained in the git repository right now is this history maintained anywhere no how do we see what all files have been changed or modified or added or deleted in short how do we know what changes have been made in the project that are not currently saved in the history of that project like no one knows those changes are being made a command for that is called git status git status basically says hey you have these two files this is some extra you know mac os file don't worry about it i can remove it so here it's saying hey kunal there's a names.txt file someone has added and it is currently untracked means if you share this project with someone like you know we have some projects shared over here this is a gina project so here you can see all the changes being made let's say some file removed or added or you know whatever all of these are maintained over here people know what things were added like add test for termination add git to docker image or whatever currently if i share this git repository on my github or this project folder on my github no one knows that names.txt file is added here by kunal on 1st August.

No one knows that. Okay? So that is basically what we want to do.

So this is the git status command that tells us that hey these are all the changes that are currently not in the history of your project. No one knows about it. Okay. Now we want to maintain these changes.

We actually want these changes to be in our project's history. People should know that Kunal created a commit that created a new file or changed a new file or deleted a file. how do we do that we can do that very easily um imagine that you are at a wedding okay now when you are at a wedding let's compare this example to a wedding example now when you are at a wedding you the you know the the couple they are on the on the stage and people uh people take their photographs like all the guests to take their photographs with the couple so how does that entire scenario play out the guests they will go on to the stage that's the number one step the photographer will then take the picture of the people or let's let's give it a bit more detail the couple's first step is going to be the couples whose photograph has not been taken they will go on the stage okay then the photographer will click their picture and then they will exit the stage and now their picture will be stay will be saved in the history of that wedding in a photo album this scenario makes sense couples who sorry guests whose photo has not been taken they will go on to the stage photographer will click the picture they will come out of the stage and their photo will be permanently saved in the history of the wedding in a photo album in this scenario how does git relate the guests whose photo has not been taken yet they are known as this thing untracked files okay what is the next We need to take a photo of these files so that we can save it in our git repository or the git history which is known as the photo album in this example.

Okay, only step that is remaining is placing all these files whose photo has not been taken yet means whose history is not being saved in the project yet onto the stage. How do we do that? There's a command for that called git add. You can either do dot.

Dot means everything in the current directory. everything in the current project directory that is currently not having its history or that is untracked means all the red red files that you saw previously put all these files in the staging area on the stage basically so that a photo of them can be taken you can also write individual names of the file let's say you have a few files that you changed but you only want to maintain the history of a few files not all the files you can also individually add the names now if i do git status again you can see it's now changed to green it means that these people whose photo has not been taken or some changes have been made let's say they got some new outfit or whatever and they want to take get their picture clicked again sometimes that happens so in that case um these people are now on the stage and we can now click their picture so now i'll be like hey photographer please click their picture so they are permanently saved in the photo album or the git history like this like this how do we do that there's another command for that git commit dash m dash m basically means Message, provide a message. Message I can provide is something like, anything you like, I can say, names.txt file added. Now, don't worry about all these things, what is master, what is this thing, what is this thing. Currently, this is very simple.

One file changed, zero insertions of lines of code, zero deletions of lines of code. That is true. I did not make any insertions of code.

All I did was create a file. That is true. Okay, so now if I do git status again, Now it's going to say, hey, are there any more people whose photograph has not been clicked yet? You tell me.

Tell it to yourself. If you said no, then you are right. Nothing to commit. Working tree clean. Since I took the last photograph, was there any other change made in this project folder?

No, there wasn't. That is why it's saying no, no other new person is there whose photo needs to be clicked. i hope that makes sense now another question you may have is so these are the basic commands git init initializes the repository git add git status and git commit as simple as it gets now you might be wondering hey let's make some other change let's say i add something in this file so i'll go in my vim i'll add unal kushfaha and i'll add rahul rana and i'll add let's say community classroom or whatever i have made changes in this file you can also double click it and see if i double click it i can see rahul rana kunal kushwaha community classroom is in this file so you can see here it is available okay there's also another command for this there's the cat names.txt it basically displays whatever things are available in the names.txt file okay three lines are available since i took the last picture or made the last commit have there been any changes in this folder structure yes names.txt file has been modified how do i know that get status so you can say you modified names.txt cool again let's take a picture of this let's get all these people who have been modified on the stage get add dot get status it basically says hey this person is now on the stage you can click their picture if you want to remove them from stage without you know committing it so let's say you commit you you you got them on the stage by mistake and you're like we don't want to take your photo your photo is already being taken so you can do git restore dash dash staged names.txt they are now again outside the stage you can see the red fold red thing okay let's say we want to git add names.txt you can see they are now on the stage i can take a picture i can say names.txt file modified now it's saying one file changed three insertions were made that is true i added i added three lines over here that is true another question you may have is hey kunal where can you see this entire history of the project all the commits that were made in the git log so currently we only have two commits kunal kushwaha made a commit on 1st august at 11 am names.txt file was added kunal kushwaha again made a commit on 1st august sunday 11 50 ist time zone plus plus 5 30 is ist time zone and names.txt file was modified i hope that makes sense and that was pretty much about it okay those are the basics of it let's make a few more commits don't worry about what the master is what you know the head is we will cover that in detail let's first look into the commit thing only okay so let's say i delete this file now if i do git status it's going to say hey this file has been deleted so i'm going to be like okay uh let me add this git add names.txt or dot git commit minus m names.txt file delete it that's it get log you can see names.txt file was deleted on first august this time imagine you did this by mistake imagine you were like hey i did this by mistake i did not want to do it all i need is i basically want to remove this commit from my history of the project so if you want to remove this commit or all the you know this commit or like these commits you cannot like remove just this particular one commit from middle because As you can see each commit has a hash id, so each commit is built on top of each other. You can unstage these commits.

let's say you want to go back to you know the previous example that we were giving let's say you want to go back to any particular history of the project let's say you want your project to be what it was like on sunday 1st august 11 47 am means this this particular commit i will just copy this thing okay if you want to remove these commits just copy the commit just below it so if i want to remove these commits i will copy this thing okay i hope that makes sense i copy this now whatever i am about to do it will remove these commits whatever commit i copy all the commits above it will be removed let's see how git reset paste if i do git log i will only be able to see one commit only one commit is here the first one that is what we wanted now you might be like hey what happened to all the files that were modified or changed or whatever in the previous commits they are now in the unstaged area this basically means that the people whose photograph was taken their photograph has been like you know deleted and now they are again in the section of people whose photograph has not yet been taken now what we what we can do is let's say we deleted this thing by mistake or whatever changes we made in these commits were by mistake then what we can do is basically we can just restore this or we can we can also put it in the staging area in the stash area for example imagine you don't want to you don't want to delete these changes right now imagine you just want to say that hey you are a few people whose photo has not yet been taken go to the backstage and whenever we want you to come back we will get you back how can i explain this more simply Let's say you are working on a project and you are working on a particular feature and you have a few few lines of code that you are working on. Now you want to try out something new with a clean code base. Okay all the changes that you have been made that are work in progress, files you have been modified, you want to remove those but you also don't want to save your progress as a separate commit.

You basically want, hey I wish there was a way where I could just put all my work somewhere else without making a commit, without making a history in the project. and whenever i want that thing back i can get that back whenever i want command for that is called git stash so basically get the people you want to be on the backstage first on the stage so it says this file was deleted this is a change now i'm going to get stashed this i'm going to say hey go back to the staging like one go into the backstage whenever i want you all the changes whenever i want all these changes being made i will bring you back for example i can make another change let's say touch surnames.txt i can say git add dot git status i can say you know um surnames dot uh names.txt this one is saying that uh names.txt has been modified to surnames.txt like renamed so it's a you know pretty smart in that way but uh what we can do is let's say let's say Let's try to make a few changes now. So vim surnames.txt.

I can say insert kushwaha. Okay, let's create another file. Let's create houses.txt. Get add dot get status.

Bunch of things I have done. Bunch of things I have done. Now what I want is like, hey, all these changes I have made, I don't want to commit it. I also don't want to lose these changes. I basically want you to go backstage and whenever I want you, I will bring you back.

You can do git stash for that. git status clean Now my project folder is exactly like it was on August 1st 1147 am. exactly like it was that was my first commit and my first commit only consisted of what? the names.txt empty file empty makes sense? that's how you can go back in the project now let's say all of these changes that you made like rename the names.txt file to houses.txt or whatever created a new file like surnames.txt or whatever you know?

how can we do that? git stash pop hey all the people who are in the backstage come back to the staging area in the unstaged area so you can see here we have all the changes that were made previously cool all right so i don't want these changes let's say i can just say git add and get stash again now let's say they are in this you know that backstage and we can just say hey your photo is not being taken go go away get stash clean sorry not clean clear so those changes that you have made that were like not committed and in the you know separate structure they are now gone you cannot get those back they're gone now okay cool so that was about how you can go back to your comments let's move forward to learning about github now so what we are going to do now is there are two ways like two things we are going to do first hosting our own project on github let's say you have your personal projects or local projects or whatever and you want to push those on github you want to share it with other people so how do we do that let's see so let me go to github.com let me open up my let's go to github we are here at github let me go to my personal profile or like the dashboard or whatever we can create a new repository i can just say create a new project i can say community classroom git or whatever i can create this repository i can set it as public and create the repository now basically this is the url for my repository gunal kushwaha community classroom git and we want this url to be attached to my project the local project a command for that is git remote add origin url i did a lot of things over here so git is just the git command remote basically means you are working with urls Add basically means you are adding a new URL. Origin basically means what is the name of the URL going to be that you are going to add. It's like phone numbers.

You don't remember all the phone numbers in your contact list. You save a phone number with the name of the person. Similarly, the name of this URL is origin. By convention, this can be anything.

But by convention, all the repositories and folders that start, that basically are in your personal account, on your own account. they have a name called origin if i say git remote minus v this will basically show me all the urls that are attached to this folder this is attached these are the same one fetch and push we'll look more into what fetch is what fetch is what fetch is and what push is and we'll look more into like what is master and head and everything don't worry about that so now our thing is connected this url is connected with this particular folder now if i refresh this can i actually see the names.txt file here let me refresh this no we can't because i have not shared the changes on this url a command for that is git push here you can see push push means push the changes to which url do you want to push origin and which branch do you want to push we'll look more into what branches are master but now imagine it's master now if i refresh this names.txt file is here commit is also here 17 minutes ago names.txt file was added this is the commit message that i sent what do we mean by branches you can see master is here currently it's known as main because of the naming convention inclusive naming convention so it's most probably going to say main whenever you get started my client is like a little old so i don't think it was updated but uh previously it was known as master what is this master what is branching let's look into it so what are branches Imagine you are making commits like we were doing. So let's make a few more random commits.

Let's say I create a new file, surnames or some other file. Hotel.txt, I can create a commit like this. Get commit, hotel.txt, added, role number.txt.

role number added let's create one more i can say i can create another file or i can just make some changes in some file i can say add some changes here something like this get status so don't worry if i'm doing it so fast because i'm just adding random commits nothing is to be explained over here roll number modified so many commits being made okay commit commit commit commit four commits being made now here you can see that this is the commit that is the latest commit on my remote branch origins master what is a branch let's look into that let's say i created four commits let's let's visualize this thing get commit get commit get commit get commit so you can see i'm making so many commits can you that this thing is being formed in sort of a branch structure that is the similarly what we are doing over here these are like linked to each other in this way and this is a branch structure okay now internally if you have heard about it this is a directed acyclic graph don't worry we will cover that in detail in the dsa bootcamp but uh as you can see as simple as it gets this does look like a branch and by default the name of this branch is called main previously it was known as master that is what this master thing represents okay so these are what are known as branches now your question might be hey kunal what is the use of a branch why are we having branches the use of it is that in short whenever you are working on some other feature or new feature or resolving a bug or whatever Always create a separate branch. Reason for that will be shared later in this video. What all things are remaining that I did not cover?

What is this head? This head thing? So in branches, what happens is that you should never commit on the main branch or the master branch, your main branch.

Because in open source project, let's say if I say React. there is the react folder facebook react or react native whatever so you can see this is the main branch this is the code that is actually being used by people like that like the users and developers imagine that you know there might be so many other branches but the main branch is the default branch this is the one that is being used by people so that is the reason we never commit directly on this because our code that is not finalized yet might contain some errors that is why all the code that is not finalized that you are working on should go on a separate branch so that the users are not affected now for that you can create a new branch like you can the command is git branch let's say feature say i'm working on a feature you can see a new branch is created called feature what do i mean by head a command was executed called git checkout feature and the star thing that was on main is now on feature this basically means that my head is now pointing to the feature branch what is this head head is just a pointer that says all the new commits that you will be made will be added on the head currently the head is on the feature branch hence all the new commits i am making will be added on the feature branch you can see color being changed and only the feature branch is moving forward main branches as it is that is what we want to do Another thing is you are not the only one who is going to be contributing to this code base. You can see so many people contribute, right?

More than 2000 people. So let's say that while you are working on a project, on your separate branch, while you are working on your issues, someone else gets their code added in the main branch. We'll talk more about how that happens. That can also happen.

So let's say git checkout main, git commit. So you can see now the main branch is going on like this. and your work is going on side by side okay this is the use of branches again do not worry how this is happening we'll cover that in detail with the completely hands-on tutorial but uh that is basically what branches are that is what basically what head is so my currently head is on the main branch whenever i make a new commit it will be now on the main branch Now, let's say your features that you are adding in your branch, in your code sample that are not currently part of the main branch means users cannot see it. Let's say these changes are now finalized. You're like, hey, people who maintain this project, you're telling them, hey, my code is now finalized.

Can you please maybe merge this code into the main code so that the users can also use it? They're going to be like, yeah, sure. Get merge feature.

Now your branch is a part of the main branch. People can actually see this code now. That was in commit C5 and C6.

okay makes sense sound good i'm doing it in such a way that uh that you all are watching live but anyway how this merging happens it happens via a pull request we'll look more into later on now i know i know there are a few questions you may having how do we create new branches why do we work with new branches why can't we just you know every why can't everyone just work with the main branch how do we get a new branch merged what is a pull request five to six questions you may be having we'll cover these shortly if you look at our code sample right now you can see that this is the one on my origins master branch okay that is true on my own repository i can just open it like this and say git remote dash v open it up only one commit is here that is true only one commit is here because that is the only thing i pushed this is the only thing i pushed if i want to push these changes on the new new like and the the new commits on the master branch i want to push that git push to which url do i want to push origin which branch do i want to push master branch refresh tada let's look at the code sample ta-da awesome four commits here four commits here head is on the master branch and origin is like the origin the the url that i added that is also over here means there are no commits above it that are extra which are not already on the url all right you can see this is my you can see this is my master branch all right now most of the questions that we need to answer right now they will be much more clearer when we are doing this next thing which is known as working with existing projects on github so let's say that this is the organization com classroom community classroom organization and this is the project of that organization known as a com classroom op for example Now, what do we need to do in order to make changes to this project and make some contributions to this project? This is the project on the account of community classroom. Should anyone who does not have access to it directly, like any random person in the world, should they be allowed to make changes to comm classroom project directly?

How dangerous would that be? so if you try it right now you will not be able to directly change any file in this folder currently it only has a readme file but if you try to change it you will not be able to do that why because you don't have access to this account this organization this is a problem that we you know face how do we resolve it what is a way to contribute to this code sample without you know because if we don't have the permission for this you create a copy of this project in your own account so i can say fork in my own account so now you can see instead of com classroom community classroom op project it is now kunal kushwaha com classroom op project so this thing you can see this folder this is the main one and this is yours main one forked one now since this folder com classroom op is in your own account you can do anything you want with it let's copy this clone and remember i said any folder that starts with your own account the name of that is going to be origin so whenever i clone this in my desktop i can say git clone url so it's basically downloading that folder In that folder, readme file is there. readme file is there.

readme file contains com.classroom.op Let's see what the readme file contains com.classroom.op Awesome, so that is the first point you cannot directly make changes to anyone's account. You have to fork it Why do we have to fork it because you can't directly make changes to anyone's project How would you feel if someone was changing the code sample in your project, you know without your permission without anything just like that That would be very dangerous But since this is a copy of that folder in your own account You can do whatever you want with it It will not reflect in the main project until unless the people who have the approval for this project can merge your code can approve your changes via a pull request which we'll look into next so this is what we're doing over here and you can see now one more thing i want to share is you know that the origin url is basically your own account from where you have forked this project means this url i repeat it again from where you have forked this project that is known as upstream url by convention so i can add another url over here git remote add upstream url origin is my personal upstream is the from where i have forked it okay let's say you can do this as well you can open a pull request after this or via via watching this video as well so you can definitely do this what i'm doing right now to get some more hands-on practice so after you have cloned this you can create, well let's do that afterwards let me first explain what a pull request is let's say i make a few commits say we make a few commits i can say kunal kush waha or i can just i can say kunal kushwaha says that this community is amazing so when you open a popl request you can add like a dash here a new point and say your name says that and whatever you feel about the community okay so when i do git status it will obviously show me that readme has been modified in the beginning i mentioned something to you that never commit on the main branch always create a new branch for whatever work you are doing let's create a new branch called kunal let's check out to that branch you all know what checkout is now checkout basically means the head will now come on the kunal branch that is true main is now changed to kunal all the commits i will make now will go on to the kunal branch git add dot git commit minus m kunal added a message git log this is the main branch not changed only till here we have the commits and the kunal branch has an extra commit now basically what i want to do is i want to push this branch i want to say the idea is that hey whatever code that i have in this branch the kunal branch of my own account please merge that into the main projects main branch that is you will do what you will do let's say rahul wants to add something so rahul will say hey rahul's account has a com classroom project and he will have a branch called rahul branch or rahul temp or whatever name he wants to give and then he will request me the person who is responsible for the main project like hey i have something let's open up some pull request let's say some pull request that i got merged previously this is an open one let's see the one that was merged so this one was merged so brad was saying like hey i have a branch known as host process Kunal, can you please merge that branch into your host process branch? Or let's look into the more simple example. This one.

So I, let's say, wanted to contribute to the Kubernetes website. This is a really nice example. I said that, hey, obviously, I cannot change your website directly.

So I created a fork of it. you can see Kunal Kushfaha Kunal Kushfaha's fork has a branch called SIG Usability Blog which contains these changes and then I am requesting the people who maintain this project like hey whatever changes I have in this branch can you please merge it into the Kubernetes main project main branch so that the other people will be able to see that and after that they were like they gave some suggestions they ran some tests I made some changes in the end my PR was merged this is what is known as a pull request so when you create your own copy and you create any change in your copy how do you make sure that this change whatever you create in your copy is visible in the main project you request it via a pull request then people will review your code suggest some changes you will make those changes and when this is merged your code sample changes that you made in your own accounts fork in any branch that will be visible in the main projects main branch so once this is merged this is merged now so basically this code sample is now in the main kubernetes website in the commits it is main website over here somewhere over here my blog is available okay that is what we're doing over here now i will give you another example on why you should never commit on master branch and always create such branches to sum up if you have any new feature you are working on on any new bug you are working on or any new thing you're working on make sure you open a new branch for that and a new pull request for that it will be much more evident when we give this example let me push this branch on github i want to push on origin if you would you all be able to push on upstream no because you don't have access to community classroom organization if you say git push upstream your own branch name it will give you error you will not be able to push it how can you push to upstream url you don't have access to it but you have access to the origin url because origin is your own account let's go to it as you there's an info there's a there's a thing this thing like all automatically it's saying us click on this this is what you need to do whenever you add yours like this you you have to make sure that hey let's say rahul is doing it so rahul's kunal rahul's com classroom copy or the fork has a branch called rahul let's say or kunal's com classroom copy has a branch called kunal and that branch contains these changes I am requesting, hey, please merge these changes into the main branch of the project. Create a new pull request. When you create a new pull request, I will get this notification.

And then I can merge this code. Before merging, I need to show you something else. Let's say I want to make some other changes.

Let's say I say touch names.txt. you can see this git commit that i am doing it's going into the main not the main branch but the kunal branch because head is at the kunal branch right now i did git checkout kunal this is something i'm just doing to show you git push origin kunal You will notice that now it will not allow me to create a new pull request. It will add these commits here only. This will now become from 1 to 2. Let's see. Let's see.

Refresh. 2. This is the reason you should never commit on main branch. Why?

Imagine you are working on 10 projects. And let's say you are working on 10 features. and for every feature you want you create just one pull request how difficult would it be to review your code to have the discussion it would be very difficult all the 10 different things on on just one pull request That is why for every new feature, every new bug or anything you are working on, create a new pull request. And from this example, you can see that if a branch already has a pull request associated with it, it will not allow you to create a new pull request. All the commits will be added to that pull request only.

In simple language, one pull request means one branch. One branch can only open one pull request. If you want to open different pull requests, for different features that you are working on in the project which is also recommended that for that you want to open different pull requests and since only one branch can be associated to one pull request so hence you will be able to need to create different branches so i hope that makes sense any particular new thing you're working on create a new branch in your local folder make a pull request from that you can have 10 branches 20 branches 100 branches whatever you like Make sure your work is distributed like this so that it does not get mixed up.

Okay, imagine one feature has 10,000 changes, 10,000 lines of changes, and you're working on 10 such features. How difficult would it be to manage such a thing? Very difficult. If you just created just one pull request for all the 10 features, don't do that. Okay, don't do that.

React Native. 381 pull requests are currently open this is doing something with pod log file this is fixing some global node this is removing some default property this is doing something with like ios imagine all of these changes that were being made let's say you know four lines of changes being made and here something like uh some other line 422 lines of changes being made we know that these 16 files that have been changed relate to this particular issue now imagine all of these field pull requests were only on one single branch and one single pr these 381 features or issues or bugs they are working on let's say there was just one pr for this how difficult would it be to manage and also many people would have to collaborate then on the single pr that does not make sense everyone is working on their own pull requests okay that is in short and now you also know why you should never commit on main branch because if you do all the commits on main branch and not create another new branches all the commits will go on the main branch only similarly if you make all the commits on the kunal branch all the commits will come over here if i want to add some new feature to this project if i add it over in this branch only it will come in this branch only which is something i don't want to do because i've already explained it multiple times make sure for every new feature bug you create new branch so that you can open a new pull request so if someone asks you why we create new branches because we want to open a new pull request and we can only open new pull requests with new branches if a branch already has a pull request open then we we don't want to we can't do that all the commits will be added here only another thing you may encounter is now this is an extra commit let's say you don't want this commit uh this this particular commit names.txt file added if i look at the code sample I look at the code sample for this. So you can see two files have been changed.

readme file was changed and names.txt was added. Let's say I want to remove this commit. The names.txt commit.

Reset thing, I did reset previously. git status, git log, sorry. So I will just copy this thing. I want to remove this, so I will copy the one below it.

git reset Now this names.txt will be unstaged unstaged I can say git add dot I can do git stash it will go back to the stash area gone my structure is now exactly like I wanted names.txt file deleted and the commit is also deleted it is also removed from the commit history when I push this I would have to force push this because the online repository contains a commit that my repository does not and I told you commits are interlinked so I will have to force push this. dash f let's refresh that commit is gone file change also to one only names.txt file is gone commit is also one only main commit that I wanted that's it. as simple as it gets that is how you work with pull requests all right cool so the next thing may be related to merging your pull request so let's say we merge this pull request i can merge this confirm merge now the changes i made in kunal kushwaha's kunal branch here they will now be represented in the main project let's see tada changes i made in my own project they are now merged into the main project i hope that makes sense okay one more thing we can notice is that um let me open up this fork again the fork is not updated that is true if you cannot directly change the account of com classroom how can com classroom directly change your account They can't do it. There are two ways to do this. You can click on this button, fetch upstream.

It basically means this branch is two commits behind the main branch. That is true. Let's look at the commits.

This basically contains two extra commits that my main branch does not contain because the extra commit that I had was in the Kunal branch. it was not in the main branch so this is the reason you might encounter something like hey some other person merged their code in the main branch i want to look at that code sample as well in my own fork how do i make sure that the main branch of the upstream and my own forks main branch is always maintained there are a few commands that you can use for that you can also use this button called fetch upstream but i will actually teach you how to do it manually first thing you need to do is git checkout your main branch so you can see here in the main branch of my fork i only have one commit but in reality in upstream it has three commits first thing we need to do is we need to fetch all these commits and changes git fetch dash dash all all the branches the prune means the ones that are deleted they will also be fetched okay now i fetched all the changes first step that was the first step second is git checkout main but you have already done that after that we can reset it reset the main branch of my origin to the main branch of upstream git reset dash dash hard upstream main branch git log three commits three commits here three commits here now my folder the fork that i have the folder that i have on my local system the main branch of that is exactly same as the one that i have in upstream over here. Let's look at my fork. It is currently also not updated because I have to push my changes. I can say git push to my origin.

Which branch do I want to push? The main branch in my local system. Time to refresh.

This branch is even with the main branch. Commits 3. Same commits in my fork. Same commits over here. that is how you keep in sync with the main branch fetch basically you can also do something like this git pull let me just do something let's say let's say we create another another commit over here okay let's say we create another commit in the main project just for demo purpose let me create another commit the ad is over here i'm creating a directly but don't like for your project you can do anything you want for your personal projects but yeah now it contains four comments because i just added one update readme and my only contains three commits okay so it's like sorry two comments so basically it's like oh no not sorry main branch three comments okay four commits three comments now basically what i want to do is you can also do something like get pull upstream main make sense this is the same thing that happened we did git fetch before git pull internally also does the same thing i just showed you so that you can understand what is what is fetch what is you know reset and whatever committing so all of these things that we did previously were done in just one single command git push sorry git pull upstream urls main branch so in the java code base whenever you want to pull the code that i submit you will just do git pull upstream main that is what you will do okay now again all the changes are in my local folder only not in my upstream so here only three commits are visible git push origin mean like hey my local folders main is in sync with the main branch the upstream branch let's also sync the origin url then refresh that are even this structure and this structure is same com classroom is OP fork and upstream is same that is about pull requests that is also about how you can get your prs merged how you can create new branches how you can make changes how you can fetch with upstream two ways i already taught you one another way is you can just fetch upstream currently it's already you know in sync so you can just click fetch and merge whenever you get a new new thing added so if i say i remove this thing let's say or i add some new commit like an exclamation mark commit i can fetch refresh it contains five commits but i only have four commits fetch and merge very simple you don't even have to do git pull upstream main just just click on this button it will do it automatically for you So I hope you are able to understand what is forking.

I hope you are able to understand why we create new branches. I hope you are able to understand how we clone, how we git add, git commit, how we keep in sync with the main branch of the upstream. I hope you know what is upstream and what is origin.

And now let's look into a few more intermediate concepts. So if you want to try it out hands-on, what I would recommend you to do is just go to this URL you know this particular one i'll leave it in the description below obviously fork it on your own account clone it get do do the same things that we did get remote add upstream url so that you can also fetch the changes whenever changes are made and then just write a line over here update your branch like whatever new branch you create and make a pull request and then i will merge your full request and your changes will be visible in the main project okay cool let's look into one more thing so let me show you something else there are two more things i want you to show one is known as merge conflict and another thing is known as squashing your commits okay so if you have a lot of commits that you are working on and you want to merge that into just one commit how do we do that so i can say git check out my kunal branch let's say or not not kunal branch some other let's say create another branch let's say great branch temp git checkout temp whenever you create a new branch your new branch is going to be created from your head so make sure whenever you create a new branch your main or the master branch is up to date whatever things that we did make sure you do that before creating a new branch okay so now let's say i want to show you how to squash commits in just one single commit so how do i do that touch one git add dot git commit minus m i can add something like one okay and then i can say touch two just creating random files uh i added two then i can say touch three added three then i can say touch four added four whatever git log consists of all these commits i want to merge all these commits in one single commit how do i do that you can use the remake rebase command for that okay a few a trick you can do it is if you want to merge all these commits or if you want to merge just these two commits for example if you want to merge all these commits you can just reset you know if i reset this this will be unstaged and then you can commit again you know that right when we when we reset this thing all the commits above it will be on the staging unstage area get them on the staging area and then just one one single commit you can make but we are going to try something different if i copy this commit and i say git rebase minus i means the interactive environment copy so you can see now all the commits above it i can either pick or squash so if i say squash squash These will these will be squashed into one single commit in the pic So this main commit that we had one these three commits will be updated in this commit Okay, squash basically means whichever one is listed as pic Merge it into the previous commit So these two will be merged here Let's say you only want to merge the middle two then you can just do these things will be merged in this one So these three will be one commit this is basically pick and squash pick basically means you are taking this commit i want this commit squash basically means above above these s whichever pick you have merge your commit in that okay let's say i want to merge all the three commits all the all these three commits in the above commit so i will make all of these as squash and this one has pick exit out of it escape colon x it will now allow you to create a message for this new commit so my message can be something like i'll add a new message over here i can say commits merged colon x back git log one two three four are gone all of them are merged in just one single commit awesome that's how you merge your commits again you can do simple git push origin whatever you want temp branch so these all files will be available over there i don't want these files so i can remove these git status so not git status git log this is what i want to remove this is the commit that created these four files copy this i can also do minus minus hard gone hard will actually reset it with hard so do it with caution okay so origin why does why does this thing keep coming no my origin is also same at the same commit upstream is also at the same commit my head is also at the same same commit uh like in the origin the head is also over there and my main branch is also here very nice okay last thing remaining is merge conflicts what are merge conflicts how we resolve those now what are merge conflicts let's look into that Merge config basically means that Let's say you changed in a file. Let's say you made a change on line number three Okay, and someone else also made a change in line number three now git will get confused Should I take your changes or should I take that person's changes? Which change do you want me to take? Git will get confused.

So git will ask you for help. Hey, please help me Tell me which change you want to take you want to take this change or you want to take that change because both of you people have modified the same line number let's see how we can imitate this thing let's try to get a merge conflict first so let's say i create a change in line number four i create a this is an example to make a merge conflict okay so i create a new pull request create a new branch and a pull request i can say Rahul made this change for example. Rahul made changes in line 4. Rahul made changes in line 4. That is okay. And now let's say, I create a new branch as well. Let's take the temp branch only.

make sense let me also change line number four so me and rahul both are changing line number four only git will get confused git will be like what are you doing which line do you want me to take you want me to take rahul's line or your line so i can say kunal made this change okay git add dot git commit minus m kunal changed line 4 then i'll just open a pull request i have to push it git push origin temp compare and pull request hey kunal's temp branch please merge it into the main branch i'll be like okay cool let's merge it create a pull request okay let's do merge config it's currently checking there you go it's giving me merge conflict now with the readme file currently it's giving me with something else but let's see what it's giving me conflict with yeah there are some conflicts over here for some reason but let's say i make let's say i resolve these for for now let's say i resolve it like let's say i check these only that's fine mark as resolved commit let's say what we want to do is that so When this PR will get merged, Rahul's PR, okay? Let's say Rahul's, this is an important point. We both have changed line number 4. Rahul changed line number 4. I also changed line number 4. Okay? Now, what is going to happen is, let's say we merge this pull request.

This is merged. Let's refresh. Now, it's giving me conflict.

It's like, hey! Rahul just got his PR merged in line number 4 and you are trying to change that only. Which one do you want me to take? That is a merge conflict.

So I would have to resolve it manually. So here you can say that Kunal made this change and here you can say Rahul made this change. this is my change in line 4 this is rahul's change so it's saying hey you want to take this one or you want to take this one i'm like okay i want to take i want to take this one i don't want the main one i want my own one mark as result commit merge okay so i overrid i did the overriding of whatever change rahul made so this change was made by rahul and i removed it because it was a branch being created no when i created it it was from the master branch so that from the main branch it's like hey rahul made a chain at line five you also made a change at line five which change do you want me to take it's like okay fine take my change when i merge this pull request in the main branch you can see now my changes are available rahul's are not available hope that makes sense cool alrighty so you can see rahul can open up vpr over here as well cool now we're going to our fork again um where is my fork so in my fork over here i can see that this is six commits behind the main branch fetch merchant merch now it's even pretty cool stuff alrighty alrighty so that was about this session and that is more than enough for you to get started even with open source contributions one last thing i would like to share is no matter how many videos you watch if you are not able to apply these things practically and actually get stuck and then resolve the doubts and everything you will not get the intuition behind it so get some hands-on practice you will get some hands-on practice when you will be downloading the code sample that i share in community classroom so basically here we have the so first thing i wanted to do is actually create a pull make a pull request in com classroom op so i actually make some changes over here i'll basically make some change like make some changes over here okay share about what you liked and everything and the next thing is uh all the changes that are all the code sample and everything i'll be submitting it will be over here so in the lectures folder here i'll be sharing all the lectures you can see git cheat sheet is already updated so all the commands and everything that we mentioned are available in this cheat sheet by github okay so rewriting history and everything some some more advanced concepts but this is more than enough for you to get started and everything you need all the code samples i'll be uploading over here for the complete data such as bootcamp make sure you share and subscribe to this video and like if you have any questions we have the discord channel all the links are in the description and uh looking forward to your pull requests and everything you know and uh yeah thanks a lot for watching and do check out the links in the description and subscribe and share with your friends and i'll see you in the next one bye