Now the first thing we're going to do is open up Terminal and inside terminal, we're going to navigate 1
to our desktop. 2
So if you're not familiar with the command line and you haven't watched the module on the command line, 3
then I recommend you to take a look at it now because we're going to be using a lot of the commands 4
to create directories and navigate around. 5
So if you're not yet familiar, then it's a good time to take a look at that. 6
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 7
directory called Story. 8
Then I'm going to cd into this Story directory. 9
And if I show you with ls, you can see that it's completely empty. 10
And similarly, Inside Finder, I can show you the same thing. 11
Let's go ahead and create some text files. 12
I'm going to use, touch, to create a file called chapter1.txt, and then I'm going to open 13
chapter1.txt 14
So let's go ahead and open chapter1 and let's write something inside, shall we? 15
Okay, so there's my masterpiece done. 16
I'm going to go ahead and hit Save and I'm going to quit. 17
So that was our first chapter, done. 18
Now let's create a Git local repository and start tracking some of these file changes. 19
To initialize git we simply write git init. 20
And as you can see, it's initialized an empty git repository inside the Story directory. 21
Now if you have a look inside finder you actually can't see this dot git at all. 22
But as we learned before, if you use, ls -a, you can see all the hidden files and you can see that .git 23
is right there and it's going to be used to track all your changes, to commit your changes and to perform 24
version control. 25
So we're currently inside the Story directory and you can also call this the Working directory. 26
So as I mentioned before, using Git and learning about version control, it comes with some of its 27
own terminology and language. 28
So I'm going to try and simplify a lot of these terms that you'll come across just so that we can all 29
be on the same page and we all understand what's going on. 30
Currently, we are inside the working directory, which is the Story directory, 31
and here, in order to start tracking the changes of my files, for example, chapter1.txt, 32
then I need to add this file to what's called a Staging Area. And that is basically an intermediate 33
place where you can pick and choose which files inside your working directory that you want to commit. 34
So to see what's currently inside your staging area, you can use the Git "status" command and it shows 35
you that there are untracked files which will be shown in red. 36
And this is something that's simply inside your working directory, but it's not yet in the staging 37
Area. In order to add it to the staging area and to start tracking changes in it, then we have to use 38
the command, "git add". 39
We're going to type, git add, and we're going to type the file name, 40
so in this case it's chapter 1.txt. 41
So go ahead and hit Enter. 42
And then if we try using Git status again, you can see that that file has been added as a new file 43
and it is now green. 44
So this is now in the staging area and it's ready to be committed. 45
So let's go ahead and commit this under version control. 46
So the command is "git commit" and I'm going to use the "-m" flag to add a commit message. 47
The commit message is really, really important. 48
It's something that helps you keep track of what changes you have made in each commit. 49
So when you create a new save point, you want to be as explicit as possible about what changes were 50
made between the last save point and this current save point. 51
For our initial commit, we can use something very simple like, "Initial commit," and this shows that 52
this is our starting point. 53
Alternatively, if you want to be slightly more specific, because in our case we've actually completed 54
chapter1, so you can write complete chapter1. 55
Now the thing that you'll realize is that usually with commit messages, they are written in the present 56
tense and this is the best practice. 57
So whereas it would probably make more sense, I guess, at least in my head anyways, to write completed 58
chapter1 as this save point, it's actually by convention that you should always use the present 59
tense. 60
So it's like you're submitting your changes now. 61
Let's go ahead and hit Enter to make our first commit and you can see what commits you have made 62
by using the Git log command. 63
You can see that this commit was made at this time by this person. 64
And it also has a hash, 65
and this hash uniquely identifies this particular commit. 66
And then right at the end, you see this commit message of what this save point was all about. 67
So now I'm going to go ahead and create two more chapters. 68
So let's just create a chapter2.txt and chapter3.txt. 69
And now we have three chapters and I'm going to go in and change some of these text files. 70
So let's say. 71
Okay, so that's chapter2 done. 72
And finally, let's go ahead and just open chapter3 and edit that as well. 73
All right. 74
So all three files have been changed. 75
And over here in Finder, you can actually get a quick peek at what the contents are, which is going 76
to be really useful for me to be able to demonstrate to you what Git is doing behind the background. 77
So now let's go ahead and add these two new files to our staging area. 78
So again, if we use Git status, you can see that there's two files that are untracked, which are 79
only in the working directory and not yet inside the staging area. 80
So we can put it into the staging area by simply adding each of them as we did before, 81
git add, and writing something like chapter2.txt and then doing, git add, chapte3.txt. 82
But as you can imagine, if you have quite a few files, then it can get incredibly tedious having to 83
do this one by one. 84
So of course there is a better way. 85
Instead of adding these files one by one, we can actually simply just say, git add, and then use the 86
dot (.) to specify everything inside this current directory. 87
So everything inside the story directory. 88
Now if I go ahead and hit enter and then let's go to, git status again, you can see that there's two 89
new files that have been added to the staging area and now we're going to commit those two files to 90
a new commit to a new save point. 91
And you know what to do. 92
If you're following along with me, go ahead and give it a go. 93
All right. 94
So how was that? 95
If you remember, the command is, git commit, and we're going to use the, -m flag, to specify a commit message 96
and we're going to write a message that is in the present tense. 97
So let's say complete chapter 2 and 3. 98
So that's everything I've done between the initial commit and this commit, 99
the only difference is the fact that I've completed now chapter 2 and chapter 3. 100
So let's go ahead and hit Enter. 101
Again, let's check it out using, git log. 102
We can see that we now have two commits, both with different hashes because they are unique and they 103
are different. 104
The initial one was Complete chapter 1 and it was done at this time. 105
And then later on, about five minutes later, I completed chapter 2 and 3 and that was the second 106
commit. 107
And this is where we are at right now. 108
So you can see by this word "HEAD", this is the position or the current state that we are in. 109
So I just want to quickly recap what we've just done. 110
We created a file in our working directory inside our story directory. 111
So the working directory is the folder or the directory where you initialize your git repository. 112
When we said "git init," we did that inside the Story directory. 113
So that becomes our working directory and from now on Git is going to try and track the changes that 114
it sees between the working directory and the local repository. 115
In the beginning, we created a file inside our working directory inside story and then we used, git add, 116
to push it to the staging area. 117
Now the reason why there is this intermediate staging area because you might wonder why not just go 118
from the working directory straight to the repository? 119
Why do we need this extra step? 120
Well, sometimes you might not want to add all of your files to be tracked or all of your files to be 121
committed. 122
So the staging area is a good place to try and figure out what are the things that you want Git to ignore 123
and what are the things that you want to be tracked? Once 124
we've used, git add, we've put our file into the staging area and we're happy with the changes that 125
we are going to commit. 126
Then the next step is to go ahead and commit it using the git commit command. 127
So now our file is inside our local repository and that version is given a name through the commit message. 128
So that means that even if we've messed up our file, we can still use the last version that's under 129
version control and we can use a special command called, git checkout, to revert back or roll back to 130
the last position in our local repository. 131
Let me show you what that looks like in the command line and how we would do that in practice. 132
At the moment I've got three nicely written chapters and I have a feeling in my book is going to be 133
a big seller. 134
Now let's say that I have, you know, been working on chapter3 and I have completely messed everything 135
up. 136
I'm just, you know, fell asleep on my keyboard and I happen to have saved my file. 137
And now if you have a look at it, it's now just mumbo jumbo. 138
And I've ruined my masterpiece. 139
But fear not, because we have version control and we have Git enabled so we have nothing to worry about. 140
I can actually revert the changes that I've made locally in my working directory. 141
So at this point, you can use git status to see that we have modifications in our chapter3.txt 142
file that have not yet been committed or added to the staging area. 143
So if we wanted to, we can actually revert this back to its previous glory. 144
But before we do that, we can use a Git command to check out what are the differences between the current 145
version of chapter 3 and the last save point in our Git repository? 146
To do that, you can use the command, "git diff". 147
So the difference, and we'll give it the chapter 3file name. 148
And if you hit Enter, you can see that this is the part that was deleted. 149
So the part in red and then this was the part that was added, which is our gobbledygook. 150
So now if I've looked at these differences, it might just be that, you know, there's only a few mistakes 151
in my new version of chapter3, and I just want to maybe copy some things over or have a look at 152
how I did certain things previously, and change my current file. 153
But other times it might be that, you know, it's just you want to torch the new file, you just don't 154
want anything to do with it 155
and you would much rather roll back to the previous version. 156
So if you want to do that, then there is a command called, "git checkout" that is going to be really, 157
really useful for you. 158
So, git checkout, and then we're going to specify the name 159
of the file that we want to check out, which is chapter3.txt. 160
And if you just watch over here, which is the preview of the current version of chapter3.txt, 161
once I hit enter on this command that basically asks to roll back this chapter three to the last version 162
that was committed in our local repository. 163
So if I hit Enter, you can see that almost immediately 164
my chapter3 has been restored to its previous glorious state, and this is the version of chapter3 165
at the last checkpoint at which I committed it. 166
So that was this one, which is completed chapter 2 and 3. 167
Whereas in this lesson we've looked mostly at local implementations of Git, 168
so saving these versions on our computer locally, 169
in the next lesson, I'm going to talk about GitHub and creating remote repositories. 170
So I'll see you there.