WEBVTT

0
00:00.230 --> 00:05.960
Now, the first thing we're going to do is open up Terminal and inside terminal, we're going to navigate

1
00:05.960 --> 00:07.560
to our desktop.

2
00:07.580 --> 00:13.640
So if you're not familiar with the command line and you haven't watched the module on the command line,

3
00:13.640 --> 00:18.260
then I recommend you to take a look at it now because we're going to be using a lot of the commands

4
00:18.260 --> 00:20.480
to create directories and navigate around.

5
00:20.480 --> 00:24.710
So if you're not yet familiar, then it's a good time to take a look at that.

6
00:25.660 --> 00:33.490
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
00:33.490 --> 00:35.560
directory called Story.

8
00:36.580 --> 00:41.020
Then I'm going to cd into this Story directory.

9
00:41.020 --> 00:45.010
And if I show you with ls, you can see that it's completely empty.

10
00:45.010 --> 00:49.030
And similarly, inside Finder, I can show you the same thing.

11
00:49.300 --> 00:53.020
Let's go ahead and create some text files.

12
00:53.020 --> 00:59.590
I'm going to use, touch, to create a file called chapter1.txt, and then I'm going to open

13
00:59.590 --> 01:00.790
chapter1.txt

14
01:03.430 --> 01:09.040
So let's go ahead and open chapter1 and let's write something inside, shall we?

15
01:12.650 --> 01:15.000
Okay, so there's my masterpiece done.

16
01:15.020 --> 01:17.930
I'm going to go ahead and hit Save and I'm going to quit.

17
01:19.080 --> 01:22.590
So that was our first chapter, done.

18
01:22.620 --> 01:30.300
Now let's create a Git local repository and start tracking some of these file changes.

19
01:30.330 --> 01:34.290
To initialize git we simply write "git init".

20
01:34.890 --> 01:41.070
And as you can see, it's initialized an empty git repository inside the Story directory.

21
01:41.100 --> 01:45.840
Now if you have a look inside finder you actually can't see this dot git at all.

22
01:45.870 --> 01:52.830
But as we learned before, if you use, ls -a, you can see all the hidden files and you can see that .git

23
01:52.860 --> 01:59.880
is right there and it's going to be used to track all your changes, to commit your changes and to perform

24
01:59.880 --> 02:00.990
version control.

25
02:00.990 --> 02:06.780
So we're currently inside the Story directory and you can also call this the Working directory.

26
02:06.780 --> 02:12.630
So as I mentioned before, using Git and learning about version control, it comes with some of its

27
02:12.630 --> 02:15.170
own terminology and language.

28
02:15.180 --> 02:19.930
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
02:19.930 --> 02:23.560
be on the same page and we all understand what's going on.

30
02:23.950 --> 02:29.350
Currently, we are inside the working directory, which is the Story directory,

31
02:29.350 --> 02:37.450
and here, in order to start tracking the changes of my files, for example, chapter1.txt,

32
02:37.480 --> 02:46.630
then I need to add this file to what's called a Staging Area. And that is basically an intermediate

33
02:46.630 --> 02:53.770
place where you can pick and choose which files inside your working directory that you want to commit.

34
02:53.800 --> 03:00.940
So to see what's currently inside your staging area, you can use the "git status" command, and it shows

35
03:00.940 --> 03:04.960
you that there are untracked files which will be shown in red.

36
03:04.960 --> 03:11.020
And this is something that's simply inside your working directory, but it's not yet in the staging

37
03:11.020 --> 03:18.310
Area. In order to add it to the staging area and to start tracking changes in it, then we have to use

38
03:18.310 --> 03:20.530
the command, "git add".

39
03:20.980 --> 03:24.520
We're going to type, git add, and we're going to type the file name,

40
03:24.520 --> 03:27.310
so in this case it's chapter 1.txt.

41
03:27.310 --> 03:29.200
So go ahead and hit Enter.

42
03:29.200 --> 03:35.800
And then if we try using "git status" again, you can see that that file has been added as a new file

43
03:35.800 --> 03:37.360
and it is now green.

44
03:37.360 --> 03:41.890
So this is now in the staging area and it's ready to be committed.

45
03:41.890 --> 03:45.610
So let's go ahead and commit this under version control.

46
03:45.610 --> 03:53.800
So the command is "git commit" and I'm going to use the "-m" flag to add a commit message.

47
03:53.890 --> 03:56.770
The commit message is really, really important.

48
03:56.770 --> 04:03.370
It's something that helps you keep track of what changes you have made in each commit.

49
04:03.370 --> 04:11.650
So when you create a new save point, you want to be as explicit as possible about what changes were

50
04:11.650 --> 04:15.220
made between the last save point and this current save point.

51
04:15.250 --> 04:22.950
For our initial commit, we can use something very simple like, "Initial commit," and this shows that

52
04:22.950 --> 04:24.600
this is our starting point.

53
04:24.630 --> 04:30.690
Alternatively, if you want to be slightly more specific, because in our case we've actually completed

54
04:30.690 --> 04:34.770
chapter1, so you can write, "Complete chapter 1".

55
04:35.610 --> 04:41.280
Now, the thing that you'll realize is that usually with commit messages, they are written in the present

56
04:41.280 --> 04:43.260
tense and this is the best practice.

57
04:43.260 --> 04:49.950
So whereas it would probably make more sense, I guess, at least in my head anyways, to write completed

58
04:49.950 --> 04:56.820
chapter1 as this save point, it's actually by convention that you should always use the present

59
04:56.850 --> 04:57.240
tense.

60
04:57.240 --> 04:59.580
So it's like you're submitting your changes now.

61
04:59.580 --> 05:07.230
Let's go ahead and hit Enter to make our first commit and you can see what commits you have made

62
05:07.230 --> 05:10.200
by using the, "git log" command.

63
05:10.410 --> 05:15.870
You can see that this commit was made at this time by this person.

64
05:15.870 --> 05:18.480
And it also has a hash,

65
05:18.480 --> 05:23.500
and this hash uniquely identifies this particular commit.

66
05:23.500 --> 05:29.530
And then right at the end, you see this commit message of what this save point was all about.

67
05:30.220 --> 05:34.930
So now I'm going to go ahead and create two more chapters.

68
05:34.930 --> 05:45.880
So let's just create a chapter2.txt  and chapter3.txt.

69
05:45.880 --> 05:52.240
And now we have three chapters and I'm going to go in and change some of these text files.

70
05:52.240 --> 05:53.860
So let's say.

71
06:05.520 --> 06:08.010
Okay, so that's chapter2 done.

72
06:08.010 --> 06:13.650
And finally, let's go ahead and just open chapter3 and edit that as well.

73
06:25.390 --> 06:25.780
All right.

74
06:25.780 --> 06:27.970
So all three files have been changed.

75
06:27.970 --> 06:33.340
And over here in Finder, you can actually get a quick peek at what the contents are, which is going

76
06:33.340 --> 06:38.830
to be really useful for me to be able to demonstrate to you what Git is doing behind the background.

77
06:38.830 --> 06:45.160
So now let's go ahead and add these two new files to our staging area.

78
06:45.160 --> 06:52.000
So again, if we use "git status", you can see that there's two files that are untracked, which are

79
06:52.000 --> 06:56.320
only in the working directory and not yet inside the staging area.

80
06:56.320 --> 07:02.950
So we can put it into the staging area by simply adding each of them as we did before,

81
07:02.980 --> 07:09.610
git add, and writing something like chapter2.txt and then doing, git add, chapte3.txt.

82
07:09.700 --> 07:16.060
But as you can imagine, if you have quite a few files, then it can get incredibly tedious having to

83
07:16.060 --> 07:17.710
do this one by one.

84
07:17.710 --> 07:20.110
So of course there is a better way.

85
07:20.260 --> 07:26.060
Instead of adding these files one by one, we can actually simply just say, "git add  .", and then use the

86
07:26.090 --> 07:31.070
dot (.) to specify everything inside this current directory.

87
07:31.070 --> 07:33.980
So everything inside the story directory.

88
07:34.070 --> 07:40.220
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
07:40.220 --> 07:47.540
new files that have been added to the staging area and now we're going to commit those two files to

90
07:47.540 --> 07:50.540
a new commit to a new save point.

91
07:50.540 --> 07:52.460
And you know what to do.

92
07:52.460 --> 07:55.520
If you're following along with me, go ahead and give it a go.

93
07:59.310 --> 07:59.700
All right.

94
07:59.700 --> 08:01.080
So how was that?

95
08:01.200 --> 08:09.570
If you remember, the command is, git commit, and we're going to use the, -m flag, to specify a commit message

96
08:09.570 --> 08:14.940
and we're going to write a message that is in the present tense.

97
08:14.940 --> 08:20.910
So let's say complete chapter 2 and 3.

98
08:21.940 --> 08:28.730
So that's everything I've done between the initial commit and this commit,

99
08:28.750 --> 08:33.200
the only difference is the fact that I've completed now chapter 2 and chapter 3.

100
08:33.220 --> 08:36.100
So let's go ahead and hit Enter.

101
08:36.760 --> 08:39.460
Again, let's check it out using, "git log".

102
08:39.460 --> 08:46.630
We can see that we now have two commits, both with different hashes because they are unique and they

103
08:46.630 --> 08:47.470
are different.

104
08:47.500 --> 08:52.930
The initial one was Complete chapter 1 and it was done at this time.

105
08:52.930 --> 08:59.590
And then later on, about five minutes later, I completed chapter 2 and 3 and that was the second

106
08:59.590 --> 09:00.370
commit.

107
09:00.370 --> 09:03.430
And this is where we are at right now.

108
09:03.430 --> 09:10.730
So you can see by this word "HEAD", this is the position or the current state that we are in.

109
09:10.750 --> 09:14.200
So I just want to quickly recap what we've just done.

110
09:14.230 --> 09:19.480
We created a file in our working directory inside our story directory.

111
09:19.480 --> 09:27.140
So the working directory is the folder or the directory where you initialize your git repository.

112
09:27.140 --> 09:31.550
When we said "git init," we did that inside the Story directory.

113
09:31.550 --> 09:37.850
So that becomes our working directory and from now on Git is going to try and track the changes that

114
09:37.850 --> 09:43.160
it sees between the working directory and the local repository.

115
09:43.790 --> 09:52.460
In the beginning, we created a file inside our working directory inside story and then we used, git add,

116
09:52.490 --> 09:54.800
to push it to the staging area.

117
09:54.830 --> 10:00.410
Now the reason why there is this intermediate staging area because you might wonder why not just go

118
10:00.410 --> 10:03.010
from the working directory straight to the repository?

119
10:03.020 --> 10:05.240
Why do we need this extra step?

120
10:05.270 --> 10:12.890
Well, sometimes you might not want to add all of your files to be tracked or all of your files to be

121
10:12.890 --> 10:13.820
committed.

122
10:13.820 --> 10:19.640
So the staging area is a good place to try and figure out what are the things that you want Git to ignore

123
10:19.640 --> 10:23.150
and, what are the things that you want to be tracked? Once

124
10:23.150 --> 10:29.210
we've used, "git add", we've put our file into the staging area and we're happy with the changes that

125
10:29.210 --> 10:30.530
we are going to commit.

126
10:30.530 --> 10:36.500
Then the next step is to go ahead and commit it using the "git commit" command.

127
10:36.740 --> 10:45.410
So now our file is inside our local repository and that version is given a name through the commit message.

128
10:45.410 --> 10:53.450
So that means that even if we've messed up our file, we can still use the last version that's under

129
10:53.450 --> 11:01.550
version control and we can use a special command called, "git checkout", to revert back or roll back to

130
11:01.550 --> 11:04.640
the last position in our local repository.

131
11:04.850 --> 11:10.130
Let me show you what that looks like in the command line and how we would do that in practice.

132
11:10.250 --> 11:15.350
At the moment I've got three nicely written chapters and I have a feeling that my book is going to be

133
11:15.350 --> 11:16.190
a big seller.

134
11:16.220 --> 11:24.200
Now let's say that I have, you know, been working on chapter3 and I have completely messed everything

135
11:24.200 --> 11:24.560
up.

136
11:24.560 --> 11:31.010
I'm just, you know, fell asleep on my keyboard and I happen to have saved my file.

137
11:31.010 --> 11:35.240
And now if you have a look at it, it's now just mumbo jumbo.

138
11:35.240 --> 11:37.490
And I've ruined my masterpiece.

139
11:37.490 --> 11:45.670
But fear not, because we have version control and we have Git enabled so we have nothing to worry about.

140
11:45.680 --> 11:51.130
I can actually revert the changes that I've made locally in my working directory.

141
11:51.140 --> 11:57.230
So at this point, you can use "git status" to see that we have modifications in our chapter3.txt

142
11:57.230 --> 12:02.150
file that have not yet been committed or added to the staging area.

143
12:02.150 --> 12:08.450
So if we wanted to, we can actually revert this back to its previous glory.

144
12:08.540 --> 12:15.500
But before we do that, we can use a Git command to check out what are the differences between the current

145
12:15.500 --> 12:20.510
version of chapter 3 and the last save point in our Git repository?

146
12:20.780 --> 12:24.140
To do that, you can use the command, "git diff".

147
12:24.140 --> 12:29.060
So the difference, and we'll give it the chapter 3file name.

148
12:29.060 --> 12:33.830
And if you hit Enter, you can see that this is the part that was deleted,

149
12:33.830 --> 12:40.520
so the part in red, and then this was the part that was added, which is our gobbledygook.

150
12:40.520 --> 12:46.400
So now if I've looked at these differences, it might just be that, you know, there's only a few mistakes

151
12:46.400 --> 12:52.460
in my new version of chapter3, and I just want to maybe copy some things over or have a look at

152
12:52.460 --> 12:57.830
how I did certain things previously, and change my current file.

153
12:57.830 --> 13:04.250
But other times it might be that, you know, it's just you want to torch the new file, you just don't

154
13:04.250 --> 13:05.510
want anything to do with it

155
13:05.510 --> 13:09.080
and you would much rather roll back to the previous version.

156
13:09.080 --> 13:15.530
So if you want to do that, then there is a command called, "git checkout" that is going to be really,

157
13:15.530 --> 13:16.910
really useful for you.

158
13:16.910 --> 13:21.020
So, git checkout, and then we're going to specify the name

159
13:21.140 --> 13:24.950
of the file that we want to check out, which is chapter3.txt.

160
13:24.950 --> 13:31.190
And if you just watch over here, which is the preview of the current version of chapter3.txt,

161
13:31.220 --> 13:39.350
once I hit Enter on this command that basically asks to roll back this chapter three to the last version

162
13:39.350 --> 13:42.610
that was committed in our local repository.

163
13:42.620 --> 13:46.070
So if I hit Enter, you can see that almost immediately

164
13:46.070 --> 13:53.270
my chapter3 has been restored to its previous glorious state, and this is the version of chapter3

165
13:53.270 --> 13:57.290
at the last checkpoint at which I committed it.

166
13:57.290 --> 14:01.670
So that was this one, which is completed chapter 2 and 3.

167
14:01.700 --> 14:07.310
Whereas in this lesson we've looked mostly at local implementations of Git,

168
14:07.310 --> 14:11.200
so saving these versions on our computer locally,

169
14:11.210 --> 14:17.000
in the next lesson, I'm going to talk about GitHub and creating remote repositories.

170
14:17.000 --> 14:18.320
So I'll see you there.