Hello everyone welcome back to the session. So in this session we are going
to learn about the Git life cycle and how it works. So without wasting any time
let's go ahead and understand the Git life cycle. So guys in Git life cycle or
about any file who exists or which exists in Git it goes through the
following stages right the file first exists in the working directory okay
once it exists in the working directory once you finalize the file we start to
stage the file for production or we start to stage the file for committing
so we stage the file using Git add command and it gets staged in the
staging area once it gets staged in the staging area
the next step would be to completely save the file inside the Git version
system by committing it right. So the next stage of the third stage of the
last stages committing the file to the Git file system okay. So three these are
the three life cycle stages of a Git file in the Git file system. Now let us
understand each of these three stages separately. So the first stage is working
directly now what is the working directory any folder in which your code
resides can be termed as a working directory, the working could or
could not be tracked by Git. It is not necessary that you know Git should be
tracking or it should be versioning your files inside the folder only then it
will be called your working directory. Any directory that you're working in
which has your coordinate will be called your working directory now you choose
which files have to be saved in the Git file system for example not all files
should be saved for example you know in your working directory you would had
some output files you would have you know you would have some input files
probably also you only want the main code to be saved in Git. The input
and the output files are arbitrary files which could also be generated while your
program is being executed right. So you don't want your input files or your
output files to be there in your Git file system that's why you have an
option to stage files. Now what is this staging area. In staging area you
basically choose which files you know you want to commit so basically imagine
it like this you know you have your working directory
and then you have a stage forget on the stage you put the files that you want to
commit so in the working directory you choose the files that you want to be
tracked by Git and once you choose those files those files automatically
become staged and hence the term staging area. So the files in the staging area
are the files which are to be committed okay. Moving further then we have commit
so once we have came down to a list which we want to commit and they are
ready in the staging area all you have to do is pass the command git commit and
all the files will now will have a new commit ID attached to them and they will
now be saved in the Git file system okay so that is what commit is all about
so it's the files are selected and ready in the staging area they can now be saved
in the repository saving a file in the repository Git is known as commit right
when we commit a repository in Git, the commit is identified by a commit ID we
already discussed this and the command for initializing this process is called
git commit so let us not get into the commands, we will talk about the commands
as we move along when we go to the commands section of Git ok so these were
the three life cycle stages of Git. Moving forward guys now let's understand
the life cycle of Git using a diagram like we discussed what working directory
is staging areas and what commit is now let us see it in action ok. So this is
your programming workspace guys and this is basically your working directory ok
now the first step to basically start saving your files or you know putting
your files in Git is to first initialize a git repository so inside
this program works with what I do is you type the command git init now what it
does it basically starts tracking your files
so now git has initialized in your repository it creates a hidden dot git
file which you basically have information of all the files which are
there in folder right now right so we have a get initialized working directory
now and inside this initialize working directory you have your files but mind
you guys these files are not yet saved in the Git file system, they're just
existing in a Git initialized file right now the next step is to basically stage
the files that you want to commit right so you choose the files that you want to
commit and you type the command Git ad followed
by the files that you want to commit and these files now get into the staging
area. The staging area is where you get you can see all the files that are ready
to be committed right once you stage the files the next step
would be to committing the files to your Git repository. The moment your commit your
file to a git repository it gets attached itself to a commit ID and now
your file is successfully saved inside the git repository but mind you guys
everything which happens from here till here
is happening inside your local system right everything is happening on the
local but Git is all about collaborative effort it's all about
teamwork it's all about you contributing to a project on which hundreds or to
hundreds or millions of people are working right so now how do you get your
file or how do you get your project which you have modified or created onto
the internet or onto the or among the people who would be ready to contribute
to it now the next thing is we need an entity where we could put our files
right we need an entity where everyone can access it so this entity is called
github right so after committing your files the next step what you can do is
you can push your files to a remote repository right and what pushing basically
means is whatever is there on your local repository all the version history
everything gets pushed or gets uploaded to a remote repository which exists on
github so it could be github it could be bit bucket it could be any platform
which could host your code but we're choosing GitHub because it's a free and
popular tool that probably each and every one who is a developer or a tester
have would have used in his lifetime right
also most I'll say most and I'll say almost every open-source project is also
actually hosted on github so that is also an awesome thing okay so this
diagram basically depicts the whole lifecycle of Git where you start from
program workspace where you have your code then you initialize it the folder
to become Git initialized working directory then once you have done with
your code what you do is you stage the files which you want to commit to the
Git repository which is called staging area and then
and commits the files to the Git file system basically the files which
were there in the staging area and once you feel that you know your repository
is ready to be seen by the world what you do is you push your files on to a
remote repository such as Github where all your teams can collaborate and add
their code as well okay so this is exactly how Git function and how the
Git lifecycle is. Our next topic is how git actually works okay so now for
understanding so we have understood the lifecycle of Git we know how you know
how a file goes through the process of a file from the part where it exists in a
folder to apart it exists in a remote repository we know the process now but
let us now understand what happens in the back end right how is Git
functioning internally let us discuss that. Now any file that is to save or
store on Git is actually stored using commit right we have only discussed this
and each commit is assigned a commit ID right so every commit that you do on
Git is assigned a unique commit ID to it through which it can basically save
itself or it can go ahead and identify okay so these six files in the states
that they are are saved as it is in this commit ID tomorrow if I change the CSV
file say I add one line or I add two lines and then I again save it on github
it will be saved inside another commit ID right but whatever you save in Git
whenever you have committed something on Git it gets saved forever you cannot
edit that now right so it gets frozen inside the commit ID okay so the first
thing that you do on Gators you commit your folder and now the folder becomes
or your repository becomes a part of get using a commit ID now if you want the
model to add a new file to your project say I add this file a CSS file to my
project I add that or and I add some code to it now when I will save my
repository it'll have seven files instead of six and will be saved inside
another commit ID okay now why are commit ID is important now for example
when I uploaded this code this code stopped working so all I want to do now
is I want to word back to the previous commit and all
I have to type in is get revert and this commit ID and my code will be up and
running again because my earlier code was running but this code had some fault
in it so rather than identifying where the fault is exactly meanwhile what I do
is I do a git revert so that my previous application is up and running and now I
can basically just do a bisection or I can basically introspect what exactly
went wrong in my code and once I've figured out what went wrong in my code I
can fix it again commit it and then my code will become life okay
so again so these are the things you can this is how basically your files are
stored inside kit now all these commits these commits basically they exist on a
branch okay now what is a branch a branch is nothing but a linear line
which is unidirectional that is all the commits which are added will be added in
front of the last comment so the starting all the branch would be commit
the first comment that you do the second commit would come after that the third
commit would come after that the fourth after that you you can never add a
commit on the previous position of the branch okay it always moves forward and
a branch also has a component called head the head of the branch always
points to the latest comment okay so if I have a commit ID which is 0 0 0 1 and
if this is the only commit which is there on the branch the head will also
point over here right the movement I add and one more commit to the branch the
head will start pointing over the earth right now if I want to refer to this
commit ID all I have to do is I can refer it to either by head or I can
refer it to by this commit ID ok so this is what a branch is now this branch is
called the master branch okay so mega-project can have multiple
branches but by default there is only one branch and whenever you initialize a
git repository and that one branch is called the master branch so any files
that you saved any commits that you made are all saved on the master branch until
unless you have one more branch that you're working on but that you have to
do manually if you're not creating any branches is simply working on gate
everything is being saved on the master branch now but why do we need branches
we have discussed what branches are you understood okay so it's a linear line
which is unidirectional which always moves forward but the point is why do we
need a plants why can't we just have comments right so we need a branch
because we use commit or we use git basically for team collaboration now for
example you know you want to enhance this code whatever code was there in
commit ID zero zero two you added a CSS you want to further enhancer and say
probably I want a login page to be also be added to this website now what you do
is you basically assign this code to a developer right and how do you assign it
you create a new branch for this feature so this branch would be called feature a
branch this branch can also be called the login page branch it's up to you
what you want to name it right so you name this branch something and now what
happens is the developer gets a new branch where a commit has now been made
which is which basically corresponds to the commit which was there on my master
branch and now what I can do is or what can this developer can do is he can work
on this code add his feature and again commit to it now when this developer
will commit back to github or when he will commit back to get his code will be
added on the feature a branch okay it will not be added on the master branch
why is it done so it is done because my master branch should always always
contain code which is ready which is which is polished which is ready for
production it should not have any incomplete code it should not have any
untested code right so that's why we isolate the development of the code from
the master branch by creating a new branch with the name of what that branch
actually does right now this developer of several developers can work on this
branch add as many comments as they want till they get the perfect finished code
and when they feel the code you know is finished is done what they do is they
commit they finally commit to this branch that this is the code that we
feel is perfect and after that the testing is also and
everything is done and if everything goes well what happens is you can merge
this branch back to the master branch okay the moment you merge this branch
back to the master branch the commit gets added on to the master branch as a
new commit and now your master branch again has completed code right so this
is how basically gate works and this is how team collaboration happens on github
right now so we discussed theoretically how basically gate works and how
different operations are different different things can happen on gate but
now we haven't seen in real practice right we don't know if I have to create
a branch how how can I actually create it and get if I have to save on file on
the gate file system if I have to initialize it Apollo how can I actually
do that right so in the next sessions you're going to talk about some of the
prominent common gate commands that you will be using in your day-to-day life
for doing some operations on gate right so in our next session we're going to
talk about the common git commands so see you in the next session have a great
day ahead and good bye