1
00:00:00,480 --> 00:00:01,560
Presenter: Now, the first thing we're gonna do

2
00:00:01,560 --> 00:00:03,600
is open up Terminal.

3
00:00:03,600 --> 00:00:07,890
And inside Terminal, we're gonna navigate to our desktop.

4
00:00:07,890 --> 00:00:10,500
So if you're not familiar with the command line

5
00:00:10,500 --> 00:00:13,860
and you haven't watched the module on the command line,

6
00:00:13,860 --> 00:00:16,560
then I recommend you to take a look at it now,

7
00:00:16,560 --> 00:00:18,300
because we're gonna be using a lot of the commands

8
00:00:18,300 --> 00:00:20,670
to create directories and navigate around.

9
00:00:20,670 --> 00:00:22,500
So if you're not yet familiar,

10
00:00:22,500 --> 00:00:24,750
then it's a good time to take a look at that.

11
00:00:25,890 --> 00:00:26,880
The first thing I'm gonna do

12
00:00:26,880 --> 00:00:29,350
is I'm going to CD into my desktop

13
00:00:31,260 --> 00:00:35,553
and here I'm going to create a new directory called Story.

14
00:00:36,870 --> 00:00:41,280
Then I'm going to cd into this Story directory.

15
00:00:41,280 --> 00:00:43,110
And if I show you with ls,

16
00:00:43,110 --> 00:00:45,150
you can see that it's completely empty.

17
00:00:45,150 --> 00:00:49,590
And similarly, inside Finder, I can show you the same thing.

18
00:00:49,590 --> 00:00:53,130
Let's go ahead and create some text files.

19
00:00:53,130 --> 00:00:57,840
I'm gonna use touch to create a file called chapter1.txt.

20
00:00:57,840 --> 00:01:01,087
And then I'm gonna open chapter1.txt.

21
00:01:03,630 --> 00:01:06,480
So let's go ahead and open chapter 1

22
00:01:06,480 --> 00:01:09,033
and let's write something inside, shall we?

23
00:01:12,750 --> 00:01:15,180
Okay, so there's my masterpiece done.

24
00:01:15,180 --> 00:01:17,883
I'm gonna go ahead and hit Save and I'm gonna quit.

25
00:01:19,320 --> 00:01:22,890
So that was our first chapter done.

26
00:01:22,890 --> 00:01:27,510
Now, let's create a Git local repository

27
00:01:27,510 --> 00:01:30,600
and start tracking some of these file changes.

28
00:01:30,600 --> 00:01:33,570
To initialize Git, we simply write git init.

29
00:01:35,220 --> 00:01:39,210
And as you can see, it's initialized an empty Git repository

30
00:01:39,210 --> 00:01:41,370
inside the Story directory.

31
00:01:41,370 --> 00:01:43,260
Now, if you have a look inside Finder,

32
00:01:43,260 --> 00:01:46,230
you actually can't see this .git at all.

33
00:01:46,230 --> 00:01:48,963
But as we learned before, if you use ls -a,

34
00:01:49,830 --> 00:01:51,510
you can see all the hidden files

35
00:01:51,510 --> 00:01:54,120
and you can see that .git is right there

36
00:01:54,120 --> 00:01:57,210
and it's going to be used to track all your changes,

37
00:01:57,210 --> 00:02:01,200
to commit your changes, and to perform version control.

38
00:02:01,200 --> 00:02:04,230
So we're currently inside the Story directory

39
00:02:04,230 --> 00:02:06,990
and you can also call this the working directory.

40
00:02:06,990 --> 00:02:08,580
So as I mentioned before,

41
00:02:08,580 --> 00:02:11,250
using Git and learning about version control,

42
00:02:11,250 --> 00:02:15,420
it comes with some of its own terminology and language.

43
00:02:15,420 --> 00:02:16,770
So I'm gonna try and simplify

44
00:02:16,770 --> 00:02:19,020
a lot of these terms that you'll come across

45
00:02:19,020 --> 00:02:21,150
just so that we can all be on the same page

46
00:02:21,150 --> 00:02:24,150
and we all understand what's going on.

47
00:02:24,150 --> 00:02:27,480
Currently, we are inside the working directory,

48
00:02:27,480 --> 00:02:29,700
which is the Story directory.

49
00:02:29,700 --> 00:02:34,700
And here in order to start tracking the changes of my files,

50
00:02:35,460 --> 00:02:40,410
for example, chapter1.txt, then I need to add this file

51
00:02:40,410 --> 00:02:43,470
to what's called a staging area.

52
00:02:43,470 --> 00:02:47,250
And that is basically a intermediate place

53
00:02:47,250 --> 00:02:49,830
where you can pick and choose which files

54
00:02:49,830 --> 00:02:53,970
inside your working directory that you want to commit.

55
00:02:53,970 --> 00:02:57,120
So to see what's currently inside your staging area,

56
00:02:57,120 --> 00:03:00,090
you can use the Git status command

57
00:03:00,090 --> 00:03:03,060
and it shows you that there are untracked files

58
00:03:03,060 --> 00:03:05,970
which will be shown in red and this is something

59
00:03:05,970 --> 00:03:08,790
that's simply inside your working directory,

60
00:03:08,790 --> 00:03:11,880
but it's not yet in the staging area.

61
00:03:11,880 --> 00:03:14,670
In order to add it to the staging area

62
00:03:14,670 --> 00:03:17,100
and to start tracking changes in it,

63
00:03:17,100 --> 00:03:21,000
then we have to use the command git add.

64
00:03:21,000 --> 00:03:24,480
We're gonna type git add and we're gonna type the file name,

65
00:03:24,480 --> 00:03:27,510
so in this case it's chapter1.txt.

66
00:03:27,510 --> 00:03:29,280
So go ahead and hit Enter.

67
00:03:29,280 --> 00:03:32,280
And then if we try using Git status, again,

68
00:03:32,280 --> 00:03:35,970
you can see that that file has been added as a new file

69
00:03:35,970 --> 00:03:37,350
and it is now green.

70
00:03:37,350 --> 00:03:39,810
So this is now in the staging area

71
00:03:39,810 --> 00:03:41,910
and it's ready to be committed.

72
00:03:41,910 --> 00:03:45,750
So let's go ahead and commit this under version control.

73
00:03:45,750 --> 00:03:49,170
So the command is git commit.

74
00:03:49,170 --> 00:03:54,000
And I'm gonna use the -m flag to add a commit message.

75
00:03:54,000 --> 00:03:56,880
The commit message is really, really important.

76
00:03:56,880 --> 00:04:00,180
It's something that helps you keep track

77
00:04:00,180 --> 00:04:03,420
of what changes you have made in each commit.

78
00:04:03,420 --> 00:04:06,180
So when you create a new save point,

79
00:04:06,180 --> 00:04:09,090
you want to be as explicit as possible

80
00:04:09,090 --> 00:04:12,300
about what changes were made

81
00:04:12,300 --> 00:04:15,390
between the last save point and this current save point.

82
00:04:15,390 --> 00:04:16,800
For our initial commit,

83
00:04:16,800 --> 00:04:20,680
we can use something very simple like Initial commit

84
00:04:21,720 --> 00:04:24,840
and this shows that this is our starting point.

85
00:04:24,840 --> 00:04:28,500
Alternatively, if you wanna be slightly more specific,

86
00:04:28,500 --> 00:04:31,830
because in our case, we've actually completed chapter 1

87
00:04:31,830 --> 00:04:34,713
so you can write Complete Chapter 1.

88
00:04:35,970 --> 00:04:37,500
Now, the thing that you'll realize

89
00:04:37,500 --> 00:04:39,420
is that usually with commit messages,

90
00:04:39,420 --> 00:04:42,000
they are written in the present tense,

91
00:04:42,000 --> 00:04:43,380
and this is the best practice.

92
00:04:43,380 --> 00:04:46,410
So whereas it would probably make more sense,

93
00:04:46,410 --> 00:04:48,360
I guess at least in my head anyways,

94
00:04:48,360 --> 00:04:52,980
to write completed chapter one as this save point.

95
00:04:52,980 --> 00:04:54,660
It's actually by convention

96
00:04:54,660 --> 00:04:57,420
that you should always use the present tense.

97
00:04:57,420 --> 00:05:00,270
So it's like you're submitting your changes now.

98
00:05:00,270 --> 00:05:04,050
Let's go ahead and hit Enter to make our first commit,

99
00:05:04,050 --> 00:05:07,320
and you can see what commits you have made

100
00:05:07,320 --> 00:05:10,650
by using the git log command.

101
00:05:10,650 --> 00:05:12,840
You can see that this commit was made

102
00:05:12,840 --> 00:05:17,840
at this time by this person, and it also has a hash

103
00:05:18,660 --> 00:05:23,660
and this hash uniquely identifies this particular commit.

104
00:05:23,670 --> 00:05:26,790
And then right at the end, you see this commit message

105
00:05:26,790 --> 00:05:29,403
of what this save point was all about.

106
00:05:30,510 --> 00:05:34,980
So now I'm gonna go ahead and create two more chapters.

107
00:05:34,980 --> 00:05:37,423
So let's just create chapter2.txt

108
00:05:41,760 --> 00:05:46,230
and chapter3.txt.

109
00:05:46,230 --> 00:05:48,390
And now we have three chapters

110
00:05:48,390 --> 00:05:52,350
and I'm gonna go in and change some of these text files.

111
00:05:52,350 --> 00:05:54,050
So let's say...

112
00:06:05,670 --> 00:06:08,310
Okay, so that's chapter 2 done.

113
00:06:08,310 --> 00:06:09,960
And finally, let's go ahead

114
00:06:09,960 --> 00:06:13,623
and just open chapter 3 and edit that as well.

115
00:06:25,470 --> 00:06:28,110
All right, so all three files have been changed

116
00:06:28,110 --> 00:06:31,560
and over here in Finder you can actually get a quick peek

117
00:06:31,560 --> 00:06:34,620
at what the contents are, which is gonna be really useful

118
00:06:34,620 --> 00:06:36,000
for me to be able to demonstrate to you

119
00:06:36,000 --> 00:06:39,150
what Git is doing behind the background.

120
00:06:39,150 --> 00:06:43,920
So now let's go ahead and add these two new files

121
00:06:43,920 --> 00:06:45,300
to our staging areas.

122
00:06:45,300 --> 00:06:47,970
So again, if we use Git status,

123
00:06:47,970 --> 00:06:51,570
you can see that there's two files that are untracked

124
00:06:51,570 --> 00:06:53,610
which are only in the working directory

125
00:06:53,610 --> 00:06:56,640
and not yet inside the staging area.

126
00:06:56,640 --> 00:06:59,460
So we can put it into the staging area

127
00:06:59,460 --> 00:07:04,200
by simply adding each of them as we did before, git add,

128
00:07:04,200 --> 00:07:06,990
and writing something like chapter2.txt

129
00:07:06,990 --> 00:07:09,990
and then doing git add chapter3.txt.

130
00:07:09,990 --> 00:07:13,530
But as you can imagine, if you have quite a few files,

131
00:07:13,530 --> 00:07:15,660
then it can get incredibly tedious

132
00:07:15,660 --> 00:07:17,940
having to do this one by one.

133
00:07:17,940 --> 00:07:20,490
So of course there is a better way.

134
00:07:20,490 --> 00:07:22,860
Instead of adding these files one by one,

135
00:07:22,860 --> 00:07:25,230
we can actually simply just say git add

136
00:07:25,230 --> 00:07:29,130
and then use the dot to specify everything

137
00:07:29,130 --> 00:07:31,170
inside this current directory,

138
00:07:31,170 --> 00:07:34,230
so everything inside the Story directory.

139
00:07:34,230 --> 00:07:36,330
Now, if I go ahead and hit Enter

140
00:07:36,330 --> 00:07:38,760
and then let's go to git status again,

141
00:07:38,760 --> 00:07:41,250
you can see that there's two new files

142
00:07:41,250 --> 00:07:44,190
that have been added to the staging area.

143
00:07:44,190 --> 00:07:47,190
And now we're going to commit those two files

144
00:07:47,190 --> 00:07:50,790
to a new commit, to a new save point,

145
00:07:50,790 --> 00:07:52,440
and you know what to do.

146
00:07:52,440 --> 00:07:54,000
If you're following along with me,

147
00:07:54,000 --> 00:07:55,353
go ahead and give it a go.

148
00:07:59,430 --> 00:08:01,530
All right, so how was that?

149
00:08:01,530 --> 00:08:05,490
If you remember, the command is git commit

150
00:08:05,490 --> 00:08:07,320
and we're going to use the m flag

151
00:08:07,320 --> 00:08:09,870
to specify a commit message

152
00:08:09,870 --> 00:08:13,020
and we're going to write a message

153
00:08:13,020 --> 00:08:15,210
that is in the present tense.

154
00:08:15,210 --> 00:08:20,210
So let's say complete chapter 2 and 3.

155
00:08:22,230 --> 00:08:24,600
So that's everything I've done

156
00:08:24,600 --> 00:08:28,890
between the initial commit and this commit.

157
00:08:28,890 --> 00:08:30,450
The only difference is the fact

158
00:08:30,450 --> 00:08:33,539
that I've completed now chapter 2 and chapter 3.

159
00:08:33,539 --> 00:08:36,092
So let's go ahead and hit Enter.

160
00:08:36,929 --> 00:08:39,630
Again, let's check it out using git log.

161
00:08:39,630 --> 00:08:43,260
We can see that we now have two commits

162
00:08:43,260 --> 00:08:45,240
both with different hashes

163
00:08:45,240 --> 00:08:47,790
because they are unique and they are different.

164
00:08:47,790 --> 00:08:50,640
The initial one was Complete Chapter 1

165
00:08:50,640 --> 00:08:53,220
and it was done at this time.

166
00:08:53,220 --> 00:08:56,430
And then later on, about five minutes later,

167
00:08:56,430 --> 00:08:58,440
I completed chapter 2 and 3

168
00:08:58,440 --> 00:09:00,690
and that was the second commit.

169
00:09:00,690 --> 00:09:03,510
And this is where we are at right now.

170
00:09:03,510 --> 00:09:06,480
So you can see by this word HEAD,

171
00:09:06,480 --> 00:09:11,010
this is the position or the current state that we are in.

172
00:09:11,010 --> 00:09:14,520
So I just wanna quickly recap what we've just done.

173
00:09:14,520 --> 00:09:17,490
We created a file in our working directory

174
00:09:17,490 --> 00:09:19,740
inside our Story directory.

175
00:09:19,740 --> 00:09:23,580
So the working directory is the folder or the directory

176
00:09:23,580 --> 00:09:27,240
where you initialize your Git repository.

177
00:09:27,240 --> 00:09:29,130
When we said git init,

178
00:09:29,130 --> 00:09:31,590
we did that inside the Story directory.

179
00:09:31,590 --> 00:09:33,870
So that becomes our working directory.

180
00:09:33,870 --> 00:09:36,120
And from now on, Git is going to try

181
00:09:36,120 --> 00:09:38,430
and track the changes that it sees

182
00:09:38,430 --> 00:09:42,963
between the working directory and the local repository.

183
00:09:43,980 --> 00:09:45,750
In the beginning, we created a file

184
00:09:45,750 --> 00:09:49,050
inside our working directory inside Story

185
00:09:49,050 --> 00:09:54,050
and then we used git add to push it to the staging area.

186
00:09:54,930 --> 00:09:58,440
Now, the reason why there is this intermediate staging area,

187
00:09:58,440 --> 00:10:00,390
'cause you might wonder why not just go

188
00:10:00,390 --> 00:10:03,120
from the working directory straight to the repository?

189
00:10:03,120 --> 00:10:05,430
Why do we need this extra step?

190
00:10:05,430 --> 00:10:07,710
Well, sometimes you might not want

191
00:10:07,710 --> 00:10:11,070
to add all of your files to be tracked

192
00:10:11,070 --> 00:10:13,950
or all of your files to be committed.

193
00:10:13,950 --> 00:10:16,020
So the staging area is a good place

194
00:10:16,020 --> 00:10:17,790
to try and figure out what are the things

195
00:10:17,790 --> 00:10:19,740
that you want git to ignore

196
00:10:19,740 --> 00:10:22,680
and what are the things that you want to be tracked.

197
00:10:22,680 --> 00:10:24,360
Once we've used git add,

198
00:10:24,360 --> 00:10:26,940
we've put our file into the staging area

199
00:10:26,940 --> 00:10:30,660
and we're happy with the changes that we are going to commit

200
00:10:30,660 --> 00:10:34,140
then the next step is to go ahead and commit it

201
00:10:34,140 --> 00:10:36,930
using the git commit command.

202
00:10:36,930 --> 00:10:41,370
So now our file is inside our local repository

203
00:10:41,370 --> 00:10:45,600
and that version is given a name through the commit message.

204
00:10:45,600 --> 00:10:48,930
So that means that even if we've messed up our file,

205
00:10:48,930 --> 00:10:52,680
we can still use the last version

206
00:10:52,680 --> 00:10:54,720
that's under version control

207
00:10:54,720 --> 00:10:56,880
and we can use a special command

208
00:10:56,880 --> 00:11:01,350
called git checkout to revert back or roll back

209
00:11:01,350 --> 00:11:04,920
to the last position in our local repository.

210
00:11:04,920 --> 00:11:07,920
Let me show you what that looks like in the command line

211
00:11:07,920 --> 00:11:10,410
and how we would do that in practice.

212
00:11:10,410 --> 00:11:13,740
At the moment, I've got three nicely written chapters

213
00:11:13,740 --> 00:11:16,290
and I have a feeling in my book it's gonna be a big seller.

214
00:11:16,290 --> 00:11:19,890
Now, let's say that I have, you know,

215
00:11:19,890 --> 00:11:21,540
been working on chapter three

216
00:11:21,540 --> 00:11:24,420
and I have completely messed everything up

217
00:11:24,420 --> 00:11:27,900
and just, you know, fell asleep on my keyboard

218
00:11:27,900 --> 00:11:31,170
and I happen to have saved my file.

219
00:11:31,170 --> 00:11:33,330
And now if you have a look at it,

220
00:11:33,330 --> 00:11:37,680
it's now just mumbo jumbo and I've ruined my masterpiece.

221
00:11:37,680 --> 00:11:40,740
But fear not because we have version control

222
00:11:40,740 --> 00:11:45,740
and we have git enabled, so we have nothing to worry about.

223
00:11:45,870 --> 00:11:48,030
I can actually revert the changes

224
00:11:48,030 --> 00:11:51,300
that I've made locally in my working directory.

225
00:11:51,300 --> 00:11:53,520
So at this point, you can use git status

226
00:11:53,520 --> 00:11:57,870
to see that we have modifications in our chapter3.txt file

227
00:11:57,870 --> 00:12:00,240
that have not yet been committed

228
00:12:00,240 --> 00:12:02,340
or added to the staging area.

229
00:12:02,340 --> 00:12:05,940
So if we wanted to, we can actually revert this

230
00:12:05,940 --> 00:12:08,790
back to its previous glory.

231
00:12:08,790 --> 00:12:12,330
But before we do that, we can use a git command

232
00:12:12,330 --> 00:12:14,610
to check out what are the differences

233
00:12:14,610 --> 00:12:17,040
between the current version of chapter 3

234
00:12:17,040 --> 00:12:20,790
and the last save point in our Git repository.

235
00:12:20,790 --> 00:12:24,270
To do that, you can use the command git diff,

236
00:12:24,270 --> 00:12:25,470
so the difference,

237
00:12:25,470 --> 00:12:29,280
and we'll give it the chapter 3 file name.

238
00:12:29,280 --> 00:12:30,360
And if you hit Enter,

239
00:12:30,360 --> 00:12:33,870
you can see that this is the part that was deleted,

240
00:12:33,870 --> 00:12:35,520
so the part in red,

241
00:12:35,520 --> 00:12:38,790
and then this was the part that was added

242
00:12:38,790 --> 00:12:40,680
which is our gobbledygook.

243
00:12:40,680 --> 00:12:43,110
So now, if I've looked at these differences,

244
00:12:43,110 --> 00:12:46,530
it might just be that, you know, there's only a few mistakes

245
00:12:46,530 --> 00:12:48,660
in my new version of chapter 3

246
00:12:48,660 --> 00:12:51,630
and I just want to maybe copy some things over

247
00:12:51,630 --> 00:12:55,410
or have a look at how I did certain things previously

248
00:12:55,410 --> 00:12:58,080
and change my current file.

249
00:12:58,080 --> 00:13:01,080
But other times it might be that, you know,

250
00:13:01,080 --> 00:13:03,300
it's just you wanna torch the new file.

251
00:13:03,300 --> 00:13:05,520
You just don't want anything to do with it

252
00:13:05,520 --> 00:13:09,330
and you would much rather roll back to the previous version.

253
00:13:09,330 --> 00:13:10,440
So if you want to do that,

254
00:13:10,440 --> 00:13:14,490
then there is a command called git checkout

255
00:13:14,490 --> 00:13:17,160
that is gonna be really, really useful for you.

256
00:13:17,160 --> 00:13:20,730
So git checkout, and then we're gonna specify

257
00:13:20,730 --> 00:13:22,890
the name of the file that we want to check out,

258
00:13:22,890 --> 00:13:25,290
which is chapter3.txt.

259
00:13:25,290 --> 00:13:28,290
And if you just watch over here, which is the preview

260
00:13:28,290 --> 00:13:31,560
of the current version of chapter3.txt,

261
00:13:31,560 --> 00:13:33,900
once I hit Enter on this command,

262
00:13:33,900 --> 00:13:37,620
that basically asks to roll back this chapter 3

263
00:13:37,620 --> 00:13:40,710
to the last version that was committed

264
00:13:40,710 --> 00:13:42,900
in our local repository.

265
00:13:42,900 --> 00:13:46,290
So if I hit Enter, you can see that almost immediately

266
00:13:46,290 --> 00:13:48,600
my chapter 3 has been restored

267
00:13:48,600 --> 00:13:51,720
to its previous glorious state,

268
00:13:51,720 --> 00:13:54,000
and this is the version of chapter 3

269
00:13:54,000 --> 00:13:57,540
at the last checkpoint at which I committed it.

270
00:13:57,540 --> 00:14:01,890
So that was this one, which is completed chapter 2 and 3.

271
00:14:01,890 --> 00:14:04,410
Whereas in this lesson we've looked mostly

272
00:14:04,410 --> 00:14:07,440
at local implementations of Git,

273
00:14:07,440 --> 00:14:11,550
so saving these versions on our computer locally,

274
00:14:11,550 --> 00:14:14,430
in the next lesson I'm gonna talk about GitHub

275
00:14:14,430 --> 00:14:17,190
and creating remote repositories.

276
00:14:17,190 --> 00:14:18,957
So I'll see you there.

