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