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