Now, the first thing we're going to do is open up Terminal and inside terminal, we're going to navigate
to our desktop.
So if you're not familiar with the command line and you haven't watched the module on the command line,
then I recommend you to take a look at it now because we're going to be using a lot of the commands
to create directories and navigate around.
So if you're not yet familiar, then it's a good time to take a look at that.
The first thing I'm going to do is I'm going to cd into my desktop, and here I'm going to create a new
directory called Story.
Then I'm going to cd into this Story directory.
And if I show you with ls, you can see that it's completely empty.
And similarly, inside Finder, I can show you the same thing.
Let's go ahead and create some text files.
I'm going to use, touch, to create a file called chapter1.txt, and then I'm going to open
chapter1.txt
So let's go ahead and open chapter1 and let's write something inside, shall we?
Okay, so there's my masterpiece done.
I'm going to go ahead and hit Save and I'm going to quit.
So that was our first chapter, done.
Now let's create a Git local repository and start tracking some of these file changes.
To initialize git we simply write "git init".
And as you can see, it's initialized an empty git repository inside the Story directory.
Now if you have a look inside finder you actually can't see this dot git at all.
But as we learned before, if you use, ls -a, you can see all the hidden files and you can see that .git
is right there and it's going to be used to track all your changes, to commit your changes and to perform
version control.
So we're currently inside the Story directory and you can also call this the Working directory.
So as I mentioned before, using Git and learning about version control, it comes with some of its
own terminology and language.
So I'm going to try and simplify a lot of these terms that you'll come across just so that we can all
be on the same page and we all understand what's going on.
Currently, we are inside the working directory, which is the Story directory,
and here, in order to start tracking the changes of my files, for example, chapter1.txt,
then I need to add this file to what's called a Staging Area. And that is basically an intermediate
place where you can pick and choose which files inside your working directory that you want to commit.
So to see what's currently inside your staging area, you can use the "git status" command, and it shows
you that there are untracked files which will be shown in red.
And this is something that's simply inside your working directory, but it's not yet in the staging
Area. In order to add it to the staging area and to start tracking changes in it, then we have to use
the command, "git add".
We're going to type, git add, and we're going to type the file name,
so in this case it's chapter 1.txt.
So go ahead and hit Enter.
And then if we try using "git status" again, you can see that that file has been added as a new file
and it is now green.
So this is now in the staging area and it's ready to be committed.
So let's go ahead and commit this under version control.
So the command is "git commit" and I'm going to use the "-m" flag to add a commit message.
The commit message is really, really important.
It's something that helps you keep track of what changes you have made in each commit.
So when you create a new save point, you want to be as explicit as possible about what changes were
made between the last save point and this current save point.
For our initial commit, we can use something very simple like, "Initial commit," and this shows that
this is our starting point.
Alternatively, if you want to be slightly more specific, because in our case we've actually completed
chapter1, so you can write, "Complete chapter 1".
Now, the thing that you'll realize is that usually with commit messages, they are written in the present
tense and this is the best practice.
So whereas it would probably make more sense, I guess, at least in my head anyways, to write completed
chapter1 as this save point, it's actually by convention that you should always use the present
tense.
So it's like you're submitting your changes now.
Let's go ahead and hit Enter to make our first commit and you can see what commits you have made
by using the, "git log" command.
You can see that this commit was made at this time by this person.
And it also has a hash,
and this hash uniquely identifies this particular commit.
And then right at the end, you see this commit message of what this save point was all about.
So now I'm going to go ahead and create two more chapters.
So let's just create a chapter2.txt and chapter3.txt.
And now we have three chapters and I'm going to go in and change some of these text files.
So let's say.
Okay, so that's chapter2 done.
And finally, let's go ahead and just open chapter3 and edit that as well.
All right.
So all three files have been changed.
And over here in Finder, you can actually get a quick peek at what the contents are, which is going
to be really useful for me to be able to demonstrate to you what Git is doing behind the background.
So now let's go ahead and add these two new files to our staging area.
So again, if we use "git status", you can see that there's two files that are untracked, which are
only in the working directory and not yet inside the staging area.
So we can put it into the staging area by simply adding each of them as we did before,
git add, and writing something like chapter2.txt and then doing, git add, chapte3.txt.
But as you can imagine, if you have quite a few files, then it can get incredibly tedious having to
do this one by one.
So of course there is a better way.
Instead of adding these files one by one, we can actually simply just say, "git add .", and then use the
dot (.) to specify everything inside this current directory.
So everything inside the story directory.
Now if I go ahead and hit enter and then let's go to, git status again, you can see that there's two
new files that have been added to the staging area and now we're going to commit those two files to
a new commit to a new save point.
And you know what to do.
If you're following along with me, go ahead and give it a go.
All right.
So how was that?
If you remember, the command is, git commit, and we're going to use the, -m flag, to specify a commit message
and we're going to write a message that is in the present tense.
So let's say complete chapter 2 and 3.
So that's everything I've done between the initial commit and this commit,
the only difference is the fact that I've completed now chapter 2 and chapter 3.
So let's go ahead and hit Enter.
Again, let's check it out using, "git log".
We can see that we now have two commits, both with different hashes because they are unique and they
are different.
The initial one was Complete chapter 1 and it was done at this time.
And then later on, about five minutes later, I completed chapter 2 and 3 and that was the second
commit.
And this is where we are at right now.
So you can see by this word "HEAD", this is the position or the current state that we are in.
So I just want to quickly recap what we've just done.
We created a file in our working directory inside our story directory.
So the working directory is the folder or the directory where you initialize your git repository.
When we said "git init," we did that inside the Story directory.
So that becomes our working directory and from now on Git is going to try and track the changes that
it sees between the working directory and the local repository.
In the beginning, we created a file inside our working directory inside story and then we used, git add,
to push it to the staging area.
Now the reason why there is this intermediate staging area because you might wonder why not just go
from the working directory straight to the repository?
Why do we need this extra step?
Well, sometimes you might not want to add all of your files to be tracked or all of your files to be
committed.
So the staging area is a good place to try and figure out what are the things that you want Git to ignore
and, what are the things that you want to be tracked? Once
we've used, "git add", we've put our file into the staging area and we're happy with the changes that
we are going to commit.
Then the next step is to go ahead and commit it using the "git commit" command.
So now our file is inside our local repository and that version is given a name through the commit message.
So that means that even if we've messed up our file, we can still use the last version that's under
version control and we can use a special command called, "git checkout", to revert back or roll back to
the last position in our local repository.
Let me show you what that looks like in the command line and how we would do that in practice.
At the moment I've got three nicely written chapters and I have a feeling that my book is going to be
a big seller.
Now let's say that I have, you know, been working on chapter3 and I have completely messed everything
up.
I'm just, you know, fell asleep on my keyboard and I happen to have saved my file.
And now if you have a look at it, it's now just mumbo jumbo.
And I've ruined my masterpiece.
But fear not, because we have version control and we have Git enabled so we have nothing to worry about.
I can actually revert the changes that I've made locally in my working directory.
So at this point, you can use "git status" to see that we have modifications in our chapter3.txt
file that have not yet been committed or added to the staging area.
So if we wanted to, we can actually revert this back to its previous glory.
But before we do that, we can use a Git command to check out what are the differences between the current
version of chapter 3 and the last save point in our Git repository?
To do that, you can use the command, "git diff".
So the difference, and we'll give it the chapter 3file name.
And if you hit Enter, you can see that this is the part that was deleted,
so the part in red, and then this was the part that was added, which is our gobbledygook.
So now if I've looked at these differences, it might just be that, you know, there's only a few mistakes
in my new version of chapter3, and I just want to maybe copy some things over or have a look at
how I did certain things previously, and change my current file.
But other times it might be that, you know, it's just you want to torch the new file, you just don't
want anything to do with it
and you would much rather roll back to the previous version.
So if you want to do that, then there is a command called, "git checkout" that is going to be really,
really useful for you.
So, git checkout, and then we're going to specify the name
of the file that we want to check out, which is chapter3.txt.
And if you just watch over here, which is the preview of the current version of chapter3.txt,
once I hit Enter on this command that basically asks to roll back this chapter three to the last version
that was committed in our local repository.
So if I hit Enter, you can see that almost immediately
my chapter3 has been restored to its previous glorious state, and this is the version of chapter3
at the last checkpoint at which I committed it.
So that was this one, which is completed chapter 2 and 3.
Whereas in this lesson we've looked mostly at local implementations of Git,
so saving these versions on our computer locally,
in the next lesson, I'm going to talk about GitHub and creating remote repositories.
So I'll see you there.