WEBVTT

0
00:00.870 --> 00:02.340
All right guys, in this lesson,

1
00:02.340 --> 00:05.250
we're going to be talking all about the .gitignore,

2
00:05.250 --> 00:06.930
and how you can set rules

3
00:06.930 --> 00:09.720
to prevent committing certain files

4
00:09.720 --> 00:12.720
to your local and remote Git repositories.

5
00:12.720 --> 00:15.990
So first things first, let's bring up terminal.

6
00:15.990 --> 00:17.610
And what we're going to do is we're going to cd

7
00:17.610 --> 00:19.560
into our desktop, and we're going

8
00:19.560 --> 00:23.010
to create a new directory here called Project.

9
00:23.010 --> 00:26.220
Just say that we have this brand new project.

10
00:26.220 --> 00:29.250
And now if we go into our project,

11
00:29.250 --> 00:32.820
and we create a whole bunch of files

12
00:32.820 --> 00:36.960
to simulate this example project.

13
00:36.960 --> 00:41.960
So let's say we've got something called file1.txt,

14
00:42.030 --> 00:43.200
and we've got a whole bunch

15
00:43.200 --> 00:46.770
of other ones, file2, file3,

16
00:46.770 --> 00:51.770
and we've also got something called secrets file.

17
00:52.560 --> 00:54.600
So we've now got four files,

18
00:54.600 --> 00:57.480
and just to illustrate the use cases

19
00:57.480 --> 01:01.230
of when we might want to have a .gitignore file,

20
01:01.230 --> 01:04.590
so say if you have this file that has some, you know,

21
01:04.590 --> 01:08.760
secret passwords or API keys,

22
01:08.760 --> 01:12.330
basically stuff that you don't want to be committed,

23
01:12.330 --> 01:15.390
especially you don't want it to be hosted

24
01:15.390 --> 01:18.000
on an open platform like GitHub.

25
01:18.000 --> 01:19.200
There have been horror stories

26
01:19.200 --> 01:24.180
of people having their Amazon AWS secret keys

27
01:24.180 --> 01:26.700
in their projects and that getting pushed

28
01:26.700 --> 01:31.170
to their remote on GitHub, and within seconds,

29
01:31.170 --> 01:33.750
their entire AWS account has been,

30
01:33.750 --> 01:36.090
basically, used up and wiped clean.

31
01:36.090 --> 01:38.580
So it's really, really important to think

32
01:38.580 --> 01:42.120
about some of these things and to be aware

33
01:42.120 --> 01:46.710
of what you are putting onto this public platform.

34
01:46.710 --> 01:48.600
Another type of file that you might want

35
01:48.600 --> 01:51.570
to add into a .gitignore, or you might want

36
01:51.570 --> 01:55.320
to not upload to GitHub are files that are to do

37
01:55.320 --> 01:58.800
with your local settings or your user preferences.

38
01:58.800 --> 02:00.510
And there's a whole bunch of these types

39
02:00.510 --> 02:04.950
of utility files that you don't really want another person

40
02:04.950 --> 02:09.210
to have to download and copy into their project folder

41
02:09.210 --> 02:13.020
if they're cloning or if they're forking your project.

42
02:13.020 --> 02:14.550
So a really common example

43
02:14.550 --> 02:18.510
that people tend to add to their .gitignore files are

44
02:18.510 --> 02:20.400
what are called .DS_Store files.

45
02:20.400 --> 02:23.040
.DS_Store files are basically a settings file

46
02:23.040 --> 02:25.830
that saves a certain things like, you know,

47
02:25.830 --> 02:28.080
how you like your icons to be arranged

48
02:28.080 --> 02:30.180
in a particular project folder.

49
02:30.180 --> 02:31.680
So let's say if we go in here,

50
02:31.680 --> 02:34.290
and we just have a look at arranged by,

51
02:34.290 --> 02:36.330
arranged by none, and then I'm going

52
02:36.330 --> 02:38.550
to move my files around,

53
02:38.550 --> 02:40.710
and let's say that this is just my preference

54
02:40.710 --> 02:42.840
for how I want my files to be shown

55
02:42.840 --> 02:45.360
inside this project folder.

56
02:45.360 --> 02:48.660
Now, the .DS_Store file is a hidden file,

57
02:48.660 --> 02:51.780
so you won't be able to see it inside your finder,

58
02:51.780 --> 02:55.830
but, of course, we as command line experts (chuckles)

59
02:55.830 --> 02:59.250
know that we can simply do a ls -a to see all

60
02:59.250 --> 03:03.030
of the hidden files that are inside our project folder.

61
03:03.030 --> 03:05.490
And you can see there it is, .DS_Store.

62
03:05.490 --> 03:07.830
So that's not something that's going to be of any use

63
03:07.830 --> 03:09.780
to anybody else, and we don't want

64
03:09.780 --> 03:13.890
to have to clutter our GitHub remote repository

65
03:13.890 --> 03:16.470
or anybody else's computer with that file.

66
03:16.470 --> 03:19.440
So we're going to add that to our .gitignore as well.

67
03:19.440 --> 03:21.150
Getting onto the point of creating

68
03:21.150 --> 03:22.980
and using a .gitignore file,

69
03:22.980 --> 03:27.450
the first thing you have to do is to make it, right?

70
03:27.450 --> 03:31.920
And to do that we're just going to create a hidden file.

71
03:31.920 --> 03:34.950
So using the dot in front of the file name,

72
03:34.950 --> 03:37.080
and it's going to be called .gitignore.

73
03:37.080 --> 03:40.650
Now the case and the wording matters a huge deal,

74
03:40.650 --> 03:42.630
because we're using Git and it's looking

75
03:42.630 --> 03:44.100
for this specific file.

76
03:44.100 --> 03:45.780
So make sure that when you're using it,

77
03:45.780 --> 03:48.120
it looks exactly like this.

78
03:48.120 --> 03:49.410
Let's create that file.

79
03:49.410 --> 03:50.850
And of course it doesn't appear,

80
03:50.850 --> 03:52.530
because it's a hidden file,

81
03:52.530 --> 03:55.200
but we can see it using ls -a,

82
03:55.200 --> 03:58.083
and we can also open it inside VS Code like this.

83
03:59.070 --> 04:00.750
Here is where we add the files

84
04:00.750 --> 04:04.680
that should be ignored when we commit our project to Git.

85
04:04.680 --> 04:06.420
Let me first show you what happens

86
04:06.420 --> 04:09.810
when we don't have anything inside our .gitignore.

87
04:09.810 --> 04:11.940
And I'm going to go through the normal process

88
04:11.940 --> 04:14.250
of setting up Git and committing it.

89
04:14.250 --> 04:17.490
First, if you remember, I have to use git init

90
04:17.490 --> 04:20.010
to initialize an empty Git repository

91
04:20.010 --> 04:22.980
inside my projects directory.

92
04:22.980 --> 04:27.330
And then I'm going to use git add . to add all the files

93
04:27.330 --> 04:29.940
that are currently inside this directory

94
04:29.940 --> 04:31.830
to the Git staging area.

95
04:31.830 --> 04:34.140
So hit Enter, and we can have a look

96
04:34.140 --> 04:38.190
at what's been added by using git status.

97
04:38.190 --> 04:41.550
And you can see that all of these files are added

98
04:41.550 --> 04:45.060
into the staging area, and they're going to be committed

99
04:45.060 --> 04:48.480
if I go ahead and do git commit.

100
04:48.480 --> 04:50.280
So that's not what we want.

101
04:50.280 --> 04:53.430
And instead, I don't want to commit the .DS_Store

102
04:53.430 --> 04:57.360
and I also don't want to commit my secrets.txt file.

103
04:57.360 --> 04:58.230
First things first,

104
04:58.230 --> 05:01.020
I'm going to basically undo what I've just done.

105
05:01.020 --> 05:06.020
So I'm going to remove all of these files from my staging area,

106
05:06.240 --> 05:09.000
and then I'm going to add them later on

107
05:09.000 --> 05:12.120
once I've got my .gitignore set up.

108
05:12.120 --> 05:14.700
Basically we're just undoing the last step.

109
05:14.700 --> 05:19.320
To do that, I'm going to write, git rm --cached,

110
05:19.320 --> 05:23.010
and I'm going to use the -r flag for recursive,

111
05:23.010 --> 05:25.650
and I'm going to use a dot to say that everything

112
05:25.650 --> 05:28.020
inside the current directory should be removed

113
05:28.020 --> 05:29.370
from the staging area.

114
05:29.370 --> 05:30.870
So now if I hit Enter,

115
05:30.870 --> 05:34.050
you can see that all of these things have been removed

116
05:34.050 --> 05:35.910
from the Git staging area,

117
05:35.910 --> 05:38.820
and if I do a git status again, you can see

118
05:38.820 --> 05:41.160
that all of these files are no longer tracked

119
05:41.160 --> 05:42.300
to be committed.

120
05:42.300 --> 05:44.310
So now this is the point where we're going to use

121
05:44.310 --> 05:45.750
that .gitignore file,

122
05:45.750 --> 05:48.210
and we're going to add the file names

123
05:48.210 --> 05:49.950
that we want to be ignored

124
05:49.950 --> 05:52.530
when we're adding and committing to Git.

125
05:52.530 --> 05:54.570
In order to add individual files,

126
05:54.570 --> 05:56.820
you can simply specify the file name

127
05:56.820 --> 05:59.370
on each and every new line.

128
05:59.370 --> 06:02.160
So, for example, if we want to ignore the .DS_Store,

129
06:02.160 --> 06:04.320
we can simply write .DS_Store.

130
06:04.320 --> 06:07.350
So exactly the same upper and lower casing

131
06:07.350 --> 06:09.420
as the name of the file.

132
06:09.420 --> 06:13.623
And if we wanted to ignore the secrets.txt file,

133
06:14.580 --> 06:17.850
then all we have to do is just to specify it

134
06:17.850 --> 06:20.190
in here on a new line.

135
06:20.190 --> 06:23.010
Now with .gitignore it has certain rules.

136
06:23.010 --> 06:28.010
So, for example, you can use the pound sign (#) to comment.

137
06:28.290 --> 06:30.450
So, for example, you can say, you know,

138
06:30.450 --> 06:34.350
"#Log files", right? To be ignored.

139
06:34.350 --> 06:37.200
Another thing you can do is use wild cards.

140
06:37.200 --> 06:41.880
So if you use the asterisk (*) and you say, *.txt, 

141
06:41.880 --> 06:44.160
that means that when you commit your project,

142
06:44.160 --> 06:48.900
all of the files that have a text extension will be ignored.

143
06:48.900 --> 06:50.670
All right, so I'm going to get rid of that,

144
06:50.670 --> 06:55.670
because I do want to commit the file1 and file2 and file3

145
06:56.152 --> 06:59.334
to Git, but I want it to ignore the .DS_Store

146
06:59.334 --> 07:01.501
and the secrets.txt files.

147
07:02.961 --> 07:06.952
Now we're ready to add our files to our staging area.

148
07:06.952 --> 07:11.035
So let's again use git add . and use git status

149
07:11.970 --> 07:13.746
to see what's been added.

150
07:13.746 --> 07:15.871
So you can see that the only files are added

151
07:15.871 --> 07:18.527
are the .gitignore, the file 1, 2, 3,

152
07:18.527 --> 07:22.980
but the .DS_Store, as well as, the secrets.txt are ignored

153
07:22.980 --> 07:26.970
as per the specifications in our .gitignore file.

154
07:26.970 --> 07:31.970
Now if I go ahead and commit this as my "Initial commit",

155
07:34.050 --> 07:36.240
then the only files are going

156
07:36.240 --> 07:40.500
to be put onto my local commit are going to be these four.

157
07:40.500 --> 07:43.320
And similarly, if I push this to GitHub,

158
07:43.320 --> 07:46.263
it will also be only these four files.

159
07:48.810 --> 07:51.480
Now let's open a new Flask project

160
07:51.480 --> 07:55.290
and let me show you how you would add a .gitignore file

161
07:55.290 --> 07:57.000
for your Python projects.

162
07:57.000 --> 07:59.520
So let's just call this Test,

163
07:59.520 --> 08:03.360
and I'm going to put it inside my desktop.

164
08:03.360 --> 08:04.860
So after I open the project,

165
08:04.860 --> 08:07.263
I'm going to create a new virtual environment.

166
08:11.040 --> 08:13.080
And I'm going to install all the modules

167
08:13.080 --> 08:15.243
from the requirements.txt file,

168
08:16.560 --> 08:18.330
then I'm going to open the terminal

169
08:18.330 --> 08:21.543
and create a .gitignore file.

170
08:22.710 --> 08:25.077
Then we're going to open our .gitignore,

171
08:25.950 --> 08:29.790
and in here I'm going to add some of the files

172
08:29.790 --> 08:33.030
that will be ignored for our Python project.

173
08:33.030 --> 08:36.090
If you go to github.com/github,

174
08:36.090 --> 08:39.900
so this is a repository owned by the GitHub team,

175
08:39.900 --> 08:42.870
they have a repository called gitignore,

176
08:42.870 --> 08:45.900
and it's basically a pre-made collection

177
08:45.900 --> 08:48.150
of useful .gitignore templates.

178
08:48.150 --> 08:50.730
And if you have a search in here,

179
08:50.730 --> 08:52.590
you can find that you've got Python,

180
08:52.590 --> 08:56.820
and you're going to see that this is a prebuilt template

181
08:56.820 --> 09:00.720
for some of the files that you probably won't want to upload

182
09:00.720 --> 09:03.930
to GitHub or any other remote repository.

183
09:03.930 --> 09:06.960
So these have the file extensions for things

184
09:06.960 --> 09:10.800
that are virtual environments or environment settings,

185
09:10.800 --> 09:13.590
and it's basically all pre-made for you.

186
09:13.590 --> 09:17.140
So you can simply just copy all of this

187
09:18.060 --> 09:21.510
and put it into your .gitignore file

188
09:21.510 --> 09:23.370
that you created earlier on.

189
09:23.370 --> 09:25.440
So just straight up paste.

190
09:25.440 --> 09:26.730
And if you remember from earlier on,

191
09:26.730 --> 09:28.590
we said that the lines that start off

192
09:28.590 --> 09:31.410
with a pound sign are the comments,

193
09:31.410 --> 09:34.440
and these ones are extensions that you would ignore.

194
09:34.440 --> 09:35.940
And you might want to add things

195
09:35.940 --> 09:38.190
like .DS_store into here as well.

196
09:38.190 --> 09:41.310
So you can have something like # Project-wide.

197
09:41.310 --> 09:45.570
So for example, the .DS_Store that we mentioned earlier on,

198
09:45.570 --> 09:49.110
and if you hit Save... Now, if we go into terminal,

199
09:49.110 --> 09:51.480
and we're inside our test folder,

200
09:51.480 --> 09:53.553
so we're going to do a git init,

201
09:55.497 --> 09:56.603
git add ., git status

202
09:59.430 --> 10:01.563
to see what's going to be committed.

203
10:02.910 --> 10:07.910
And then we're going to say, git commit -m "Initial commit".

204
10:10.140 --> 10:14.010
There we go, and now, once you're ready to commit

205
10:14.010 --> 10:16.350
and add all of this to your remote,

206
10:16.350 --> 10:20.790
then it will ignore all of these common user-related files

207
10:20.790 --> 10:24.330
that are not going to be useful to your collaborators

208
10:24.330 --> 10:27.420
or anybody who wants to contribute on GitHub.

209
10:27.420 --> 10:31.019
That was just a quick tidbit about using .gitignore,

210
10:31.019 --> 10:33.570
and I know that some of you guys have been asking

211
10:33.570 --> 10:36.630
about, you know, where should you put your API keys,

212
10:36.630 --> 10:39.120
where should you put your sensitive data?

213
10:39.120 --> 10:40.230
And you have to make sure

214
10:40.230 --> 10:42.660
that when you are uploading your projects

215
10:42.660 --> 10:45.360
with these secret keys and passwords,

216
10:45.360 --> 10:47.850
to make sure that you set up a .gitignore file,

217
10:47.850 --> 10:49.893
and to include those things in there.

218
10:51.000 --> 10:53.820
All right, so that's all for this lesson.

219
10:53.820 --> 10:56.880
In the next lesson I want to talk about Git Clone,

220
10:56.880 --> 11:01.530
and how to clone various repositories to your local system.

221
11:01.530 --> 11:04.230
So for all of that and more in the next lesson.

222
11:04.230 --> 11:05.330
So I'll see you there.