[MUSIC]
Let us now explore some basic Git commands that are very useful for
us in this specialization.
There's a lot more to get than what we cover in this exercise.
To get started go to a convenient location on your computer and
then create a folder named git-test.
Then, open this folder in your favorite editor.
Here, I have the git-test folder that we just created open
in my VIsual Studio code.
Let me add a file to this
folder named index.html.
Now you can see that I have added in some HTML code into this index toward HTML5.
Let's save the changes.
Now let's switch to our command line.
At the command line, go to the git-test folder and
let's initialize this folder to be a git repository.
So to do that, at the command line, type git init.
Now this folder has been initialized to be a git repository.
This is our first git command that we have learned, git init.
This intializes the current folder as a git repository and
when it initializes the folder, it will mark that folder as a master.
This is the master branch for my git.
Now let's not worry about branches and so on.
We will not deal with that in this course.
We will only be working with the master branch in this particular specialization.
So this would be marked as a master.
Now this is the initial point of our repository.
The next command that we're going to look at is git status.
If you type git status in the command line,
it'll tell you the current status of the folder.
So let's do that at the command line and see what it shows.
At the command line, type git status and
read the information that is tracked out on the command window.
You see that it says on branch master.
So, that is the master branch that we are on and it says,
untracked files, and then shows index.html in red.
On your specific computer, it may be using different colors or
represent this differently, but this is what it shows in my Mac.
So now, this index.html file that we have just created in this folder
is now not been added to our git repository.
So let's go ahead and add that file to the git repository.
So to do that we say git add and you can simply say
dot which means that all the files in the current directory will be added to
what is called the staging area of my git repository.
So now if I again type git status,
you will see that the file index.html is marked in green.
And it says changes to be committed there, and then shows the filename.
And then, so that means that this file is ready to be committed to my
git repository.
So the next command that we saw was git add,
by using git add you can add file or folders to that staging area.
So once you add it to the staging area, then you can commit that snapshot
of our folder status to our git repository.
So that means that when you do that git commit.
What this command will do is commit the current
state of our folders into our git repository.
So all the files, as they exist at the moment,
once they have been stages using the git add, then they will be committed to
our git repository when we execute the git command folder.
So on this point, when we execute the git command, then our initial
state will now be changed to the first commit to the git repository.
So let's go ahead and do that.
Back at the command prompt, let's type,
git commit and then we can ever add a message to our commit.
So I am going to say git commit -m first commit,
because this is our first commit to our git repository.
So, when I do that, it says, okay one file has been added to the git repository and
some other information will be typed out onto that command window.
So let's now check again, git status.
And now you see that it says nothing to commit, working directory is clean.
So what that means is that the current state of my working directory, or
working folder, has been committed to the git repository.
So a snapshot has been committed to my git repository.
Now I can type the next command called git log --oneline.
And see that it shows a number there, an eight digit number there.
And then also shows the message that we put in to our commit, saying first commit.
So that is the log of all the commits that have been put into my data repository.
So going back to our next git command we saw that git log --oneline we'll see and
it shows us a brief log of all the commits.
If you simply type git log it display a lot more detail information about
all the commits.
But this is sufficient enough for obtaining information that we require.
Let's now come back to our editor here in Visual Studio code.
So I'm going to add more changes to my index.html file.
So I will add.
So I have changed my index.html file.
Now let me add another folder under the GIT-TEST folder.
So I will create a subfolder here named templates, and
inside this templates folder, I'm going to create another file named test.html.
This is just to show you how git can commit
entire folder hierarchy into its repository.
So with test.html now there,
I'm just going to copy everything from my index.html
into my test.html, and save the changes.
Going back to the command line,
let's now check out the status of our git repository and this folder.
So typing git status shows that the index.html
file that we have already added earlier to the repository has now been modified.
So if there is a new version of the index index.html file.
Also it shows that there are some untracked files in
this folder called templates.
Let's add all these changes to our repository, to the staging area.
So again type git add dot and then all these
files will be added to the staging area.
Again, checking out the status,
you now see that the changes that I've been added to the staging area.
So all these files have been added to the staging area.
Let's do one more commit.
So I would say git commit -m.
Second commit and then let's check out the log.
If you check out the log you now see that there two commits in my git repository.
The first and the second commit and
note that each one of them is given a different number there.
If you want to see the full details of the log, you can type simply git log and
then you will see more details in there then what you would be interested in.
So notice that the oneline commit
only gives the first few characters of my commit number there.
That is sufficient enough for us to operate with.
Let me now go back again to my Visual Studio and to add one more.
One more line to my index.html file.
So now my index.html file has been modified.
And let's save the changes.
Going back to the command line,
go in git status shows that the index.html file has been modified.
So let's add this to staging area and then do a third commit.
So let's say git add dot git status.
Now you see that the index.html, the modified version has been added.
Now we can say git commit.
Third commit.
And git log oneline, and you'll see that there are three commits in our repository.
So now our repository contains snapshots of three different types.
At the end of the first commit, at the end of the second commit, and
at the end of the third commit.
Now we also can roll back changes.
We can revert the repository to a previous version.
We can pull out a file out from an older commit and
then ripple this existing file in our directly from the older commit.
So let’s see how we can operate with these things by learning a couple more commands.
At this stage, our index.HTML file is in the current state.
So you can notice that it has an h1 and two p's.
Let's now look at the next git command.
The next git command that we're going to learn about is git checkout.
So this checkout command allows us to checkout a file
from a previous commit in our git repository.
So if we don't like the current file that we have in our folder and
we will want to go back to a previous version of the file,
we can always check out the file from a previous commit, or
from the current commit and then continue to work with that file.
So let's make use of this and see some folder changes to our git repository.
Going back to our command line, we remember that between the second and
the third commit I made changes to my index.html file.
Suppose I want to revert back to the index.html file from my second commit.
So then I can simply say git checkout 900cfcf.
So that is commit identifier, the number that identifies that particular comment.
And then I can say, index.html and what you would notice
is the back older file will now be checked up into my current working directory.
Going to my Visual Studio code,
you now notice that my index.html file has reverted to the previous version.
So the change that made before the third commit is now gone.
So my index.html file has been restored to
it's a state at the end of the second comment.
Now at the command line if I type git status.
You'll notice like this index.html file.
Which has reverted to what it was at the end of the second commit.
It has now already been staged so using the git checkout
we'll pull out an older version of the file and then replace what
is in the current directory and then it will also check it into the staging area.
So now if I do that and then I realize that this is what I want I can simply
do another commit at this point.
And then that file can be commented as the fourth commit.
But suppose I don't like this, I want to revert back to that
index.html file at the end of the third commit.
Then all I can do is say git reset HEAD and
index.html.
So at this point what happens is that the index.html,
the modified version that I check out is still there.
But this file has been unstaged from the staging area.
If you go back and look at the index.html in your editor,
it will still show the stage at the end of the second commit
because we had pull down that file using check out for that.
Now if you want to revert it back to what it was at the end of the third commit
then we do one more check out from the third commit.
Going to our command window type git status and
you would notice that the index.html is marked as modified, but
it also shows this particular statement here, it says git checkout --
and the file link to discard the changes in the working directory.
So that's one way you can discard the changes that you
don't need to a particular file corresponding to the previous commit.
So let me restore this index.html back
to what it was at the end of the third commit.
So to do that I will simply say git
checkout -- index.html.
And then, if I do git status it shows that my directory is clean and, basically
my directory had been restored to the state of the end of the third commit.
Going to the file in my Visual Studio code I see that
the file has been restored back to what it was at the end of the third commit.
So this is one way you can, if you've made changes to a file after the commit and
you want to just discard those changes you can simply
check out the file from the last commit.
And then all your changes that you've done after the last commit
will be discarded on the spot.
So these are some basic commands that are very useful for
you as you go to the courses in this specialization,
because you may want to commit at the end of each exercise.
And as you proceed forward you would still have a committed version of
the state of your folder at the end of the previous exercise.
So that way if you are getting out a new exercise and you discovered that you
had made mistakes and you want to revert back to the previous commit you
always have a way of doing that using the commands that we have just learned.
So with this basic understanding of these few git commands,
we will be able to proceed forward with understanding and
using git in the courses of this specialization.
So now, we have review the git reset for specific file or git reset
in generally if you simply type git reset it'll restore you back to the last commit.
So it would reset the staging area to the last commit
without disturbing the changes that you have done to your working directory.
So once you reset, then you can check out
the previous version of the file that you have committed in the previous commit.
So this way you can restore your folder back to where you were at
the starting point of the previous commit.
So sometimes when you are going an exercise and you realize you made mistake
you always have a way of reverting back to a previous version.
So with this commands, I think you're all set to use git in
the courses of this specialization So
at the end of this exercise, did you Git it?
[MUSIC]