1
00:00:00,870 --> 00:00:02,340
Instructor: All right guys, in this lesson,

2
00:00:02,340 --> 00:00:05,250
we're gonna be talking all about the gitignore,

3
00:00:05,250 --> 00:00:06,930
and how you can set rules

4
00:00:06,930 --> 00:00:09,720
to prevent committing certain files

5
00:00:09,720 --> 00:00:12,720
to your local and remote Git repositories.

6
00:00:12,720 --> 00:00:15,990
So first things first, let's bring up terminal.

7
00:00:15,990 --> 00:00:17,610
And what we're gonna do is we're gonna cd

8
00:00:17,610 --> 00:00:19,560
into our desktop, and we're going

9
00:00:19,560 --> 00:00:23,010
to create a new directory here called Project.

10
00:00:23,010 --> 00:00:26,220
Just say that we have this brand new project.

11
00:00:26,220 --> 00:00:29,250
And now if we go into our project,

12
00:00:29,250 --> 00:00:32,820
and we create a whole bunch of files

13
00:00:32,820 --> 00:00:36,960
to simulate this example project.

14
00:00:36,960 --> 00:00:41,960
So let's say we've got something called file2.txt,

15
00:00:42,030 --> 00:00:43,200
and we've got a whole bunch

16
00:00:43,200 --> 00:00:46,770
of other ones, file2, file3,

17
00:00:46,770 --> 00:00:51,770
and we've also got something called secrets file.

18
00:00:52,560 --> 00:00:54,600
So we've now got four files,

19
00:00:54,600 --> 00:00:57,480
and just to illustrate the use cases

20
00:00:57,480 --> 00:01:01,230
of when we might want to have a gitignore file.

21
00:01:01,230 --> 00:01:04,590
So say if you have this file that has some, you know,

22
00:01:04,590 --> 00:01:08,760
secret passwords or API keys,

23
00:01:08,760 --> 00:01:12,330
basically stuff that you don't want to be committed,

24
00:01:12,330 --> 00:01:15,390
especially you don't want it to be hosted

25
00:01:15,390 --> 00:01:18,000
on an open platform like GitHub.

26
00:01:18,000 --> 00:01:19,200
There have been horror stories

27
00:01:19,200 --> 00:01:24,180
of people having their Amazon AWS secret keys

28
00:01:24,180 --> 00:01:26,700
in their projects and that getting pushed

29
00:01:26,700 --> 00:01:31,170
to their remote on GitHub, and within seconds,

30
00:01:31,170 --> 00:01:33,750
their entire AWS account has been,

31
00:01:33,750 --> 00:01:36,090
basically, used up and wiped clean.

32
00:01:36,090 --> 00:01:38,580
So it's really, really important to think

33
00:01:38,580 --> 00:01:42,120
about some of these things and to be aware

34
00:01:42,120 --> 00:01:46,710
of what you are putting onto this public platform.

35
00:01:46,710 --> 00:01:48,600
Another type of file that you might want

36
00:01:48,600 --> 00:01:51,570
to add into a gitignore, or you might want

37
00:01:51,570 --> 00:01:55,320
to not upload to GitHub are files that are to do

38
00:01:55,320 --> 00:01:58,800
with your local settings or your user preferences.

39
00:01:58,800 --> 00:02:00,510
And there's a whole bunch of these types

40
00:02:00,510 --> 00:02:04,950
of utility files that you don't really want another person

41
00:02:04,950 --> 00:02:09,210
to have to download and copy into their project folder

42
00:02:09,210 --> 00:02:13,020
if they're cloning or if they're forking your project.

43
00:02:13,020 --> 00:02:14,550
So a really common example

44
00:02:14,550 --> 00:02:18,510
that people tend to add to their gitignore files are

45
00:02:18,510 --> 00:02:20,400
what are called .DS_Store files.

46
00:02:20,400 --> 00:02:23,040
DS store files are basically a settings file

47
00:02:23,040 --> 00:02:25,830
that saves a certain things like, you know,

48
00:02:25,830 --> 00:02:28,080
how you like your icons to be arranged

49
00:02:28,080 --> 00:02:30,180
in a particular project folder.

50
00:02:30,180 --> 00:02:31,680
So let's say if we go in here,

51
00:02:31,680 --> 00:02:34,290
and we just have a look at arranged by,

52
00:02:34,290 --> 00:02:36,330
arranged by none, and then I'm going

53
00:02:36,330 --> 00:02:38,550
to move my files around,

54
00:02:38,550 --> 00:02:40,710
and let's say that this is just my preference

55
00:02:40,710 --> 00:02:42,840
for how I want my files to be shown

56
00:02:42,840 --> 00:02:45,360
inside this project folder.

57
00:02:45,360 --> 00:02:48,660
Now the DS Store file is a hidden file,

58
00:02:48,660 --> 00:02:51,780
so you won't be able to see it inside your finder,

59
00:02:51,780 --> 00:02:55,830
but, of course, we as command line experts (chuckles)

60
00:02:55,830 --> 00:02:59,250
know that we can simply do a ls -a to see all

61
00:02:59,250 --> 00:03:03,030
of the hidden files that are inside our project folder.

62
00:03:03,030 --> 00:03:05,490
And you can see there it is, .DS_Store.

63
00:03:05,490 --> 00:03:07,830
So that's not something that's gonna be of any use

64
00:03:07,830 --> 00:03:09,780
to anybody else, and we don't want

65
00:03:09,780 --> 00:03:13,890
to have to clutter our GitHub remote repository

66
00:03:13,890 --> 00:03:16,470
or anybody else's computer with that file.

67
00:03:16,470 --> 00:03:19,440
So we're gonna add that to our gitignore as well.

68
00:03:19,440 --> 00:03:21,150
Getting onto the point of creating

69
00:03:21,150 --> 00:03:22,980
and using a gitignore file,

70
00:03:22,980 --> 00:03:27,450
the first thing you have to do is to make it, right?

71
00:03:27,450 --> 00:03:31,920
And to do that we're just going to create a hidden file.

72
00:03:31,920 --> 00:03:34,950
So using the dot in front of the file name,

73
00:03:34,950 --> 00:03:37,080
and it's gonna be called gitignore.

74
00:03:37,080 --> 00:03:40,650
Now the case and the wording matters a huge deal,

75
00:03:40,650 --> 00:03:42,630
because we're using Git and it's looking

76
00:03:42,630 --> 00:03:44,100
for this specific file.

77
00:03:44,100 --> 00:03:45,780
So make sure that when you're using it,

78
00:03:45,780 --> 00:03:48,120
it looks exactly like this.

79
00:03:48,120 --> 00:03:49,410
Let's create that file.

80
00:03:49,410 --> 00:03:50,850
And of course it doesn't appear,

81
00:03:50,850 --> 00:03:52,530
because it's a hidden file,

82
00:03:52,530 --> 00:03:55,200
but we can see it using ls -a,

83
00:03:55,200 --> 00:03:58,083
and we can also open it inside VS Code like this.

84
00:03:59,070 --> 00:04:00,750
Here is where we add the files

85
00:04:00,750 --> 00:04:04,680
that should be ignored when we commit our project to Git.

86
00:04:04,680 --> 00:04:06,420
Let me first show you what happens

87
00:04:06,420 --> 00:04:09,810
when we don't have anything inside our gitignore.

88
00:04:09,810 --> 00:04:11,940
And I'm gonna go through the normal process

89
00:04:11,940 --> 00:04:14,250
of setting up Git and committing it.

90
00:04:14,250 --> 00:04:17,490
First, if you remember, I have to use git init

91
00:04:17,490 --> 00:04:20,010
to initialize an empty Git repository

92
00:04:20,010 --> 00:04:22,980
inside my projects directory.

93
00:04:22,980 --> 00:04:27,330
And then I'm gonna use git add dot to add all the files

94
00:04:27,330 --> 00:04:29,940
that are currently inside this directory

95
00:04:29,940 --> 00:04:31,830
to the Git staging area.

96
00:04:31,830 --> 00:04:34,140
So hit enter, and we can have a look

97
00:04:34,140 --> 00:04:38,190
at what's been added by using git status.

98
00:04:38,190 --> 00:04:41,550
And you can see that all of these files are added

99
00:04:41,550 --> 00:04:45,060
into the staging area, and they're going to be committed

100
00:04:45,060 --> 00:04:48,480
if I go ahead and do git commit.

101
00:04:48,480 --> 00:04:50,280
So that's not what we want.

102
00:04:50,280 --> 00:04:53,430
And instead, I don't want to commit the .DS_Store

103
00:04:53,430 --> 00:04:57,360
and I also don't want to commit my secrets.txt file.

104
00:04:57,360 --> 00:04:58,230
First things first,

105
00:04:58,230 --> 00:05:01,020
I'm going to basically undo what I've just done.

106
00:05:01,020 --> 00:05:06,020
So I'm gonna remove all of these files from my staging area,

107
00:05:06,240 --> 00:05:09,000
and then I'm going to add them later on

108
00:05:09,000 --> 00:05:12,120
once I've got my gitignore set up.

109
00:05:12,120 --> 00:05:14,700
Basically we're just undoing the last step.

110
00:05:14,700 --> 00:05:19,320
To do that, I'm gonna write git remove --cached,

111
00:05:19,320 --> 00:05:23,010
and I'm gonna use the r flag for recursive,

112
00:05:23,010 --> 00:05:25,650
and I'm gonna use a dot to say that everything

113
00:05:25,650 --> 00:05:28,020
inside the current directory should be removed

114
00:05:28,020 --> 00:05:29,370
from the staging area.

115
00:05:29,370 --> 00:05:30,870
So now if I hit enter,

116
00:05:30,870 --> 00:05:34,050
you can see that all of these things have been removed

117
00:05:34,050 --> 00:05:35,910
from the Git staging area,

118
00:05:35,910 --> 00:05:38,820
and if I do a git status again, you can see

119
00:05:38,820 --> 00:05:41,160
that all of these files are no longer tracked

120
00:05:41,160 --> 00:05:42,300
to be committed.

121
00:05:42,300 --> 00:05:44,310
So now this is the point where we're gonna use

122
00:05:44,310 --> 00:05:45,750
that gitignore file,

123
00:05:45,750 --> 00:05:48,210
and we're gonna add the file names

124
00:05:48,210 --> 00:05:49,950
that we want to be ignored

125
00:05:49,950 --> 00:05:52,530
when we're adding and committing to Git.

126
00:05:52,530 --> 00:05:54,570
In order to add individual files,

127
00:05:54,570 --> 00:05:56,820
you can simply specify the file name

128
00:05:56,820 --> 00:05:59,370
on each and every new line.

129
00:05:59,370 --> 00:06:02,160
So, for example, if we want to ignore the .DS_Store,

130
00:06:02,160 --> 00:06:04,320
we can simply write .DS_Store.

131
00:06:04,320 --> 00:06:07,350
So exactly the same upper and lower casing

132
00:06:07,350 --> 00:06:09,420
as the name of the file.

133
00:06:09,420 --> 00:06:13,623
And if we wanted to ignore the secrets.txt file,

134
00:06:14,580 --> 00:06:17,850
then all we have to do is just to specify it

135
00:06:17,850 --> 00:06:20,190
in here on a new line.

136
00:06:20,190 --> 00:06:23,010
Now with gitignore it has certain rules.

137
00:06:23,010 --> 00:06:28,010
So, for example, you can use the pound sign to comment.

138
00:06:28,290 --> 00:06:30,450
So, for example, you can say, you know,

139
00:06:30,450 --> 00:06:34,350
log files, right, to be ignored.

140
00:06:34,350 --> 00:06:37,200
Another thing you can do is use wild cards.

141
00:06:37,200 --> 00:06:41,880
So if you use the asterisk and you say asterisk.txt

142
00:06:41,880 --> 00:06:44,160
that means that when you commit your project,

143
00:06:44,160 --> 00:06:48,900
all of the files that have a text extension will be ignored.

144
00:06:48,900 --> 00:06:50,670
All right, so I'm gonna get rid of that,

145
00:06:50,670 --> 00:06:55,670
because I do want to commit the file1 and file2 and file3

146
00:06:56,152 --> 00:06:59,334
to Git, but I want it to ignore the .DS_Store

147
00:06:59,334 --> 00:07:01,501
and the secrets.txt files.

148
00:07:02,961 --> 00:07:06,952
Now we're ready to add our files to our staging area.

149
00:07:06,952 --> 00:07:11,035
So let's again use git add dot and use Git status

150
00:07:11,970 --> 00:07:13,746
to see what's been added.

151
00:07:13,746 --> 00:07:15,871
So you can see that the only files are added

152
00:07:15,871 --> 00:07:18,527
are the gitignore, the file 1, 2, 3,

153
00:07:18,527 --> 00:07:22,980
but the .DS_Store as well as the secrets.txt are ignored

154
00:07:22,980 --> 00:07:26,970
as per the specifications in our gitignore file.

155
00:07:26,970 --> 00:07:31,970
Now if I go ahead and commit this as my initial commit,

156
00:07:34,050 --> 00:07:36,240
then the only files are going

157
00:07:36,240 --> 00:07:40,500
to be put onto my local commit are going to be these four.

158
00:07:40,500 --> 00:07:43,320
And similarly, if I push this to GitHub,

159
00:07:43,320 --> 00:07:46,263
it will also be only these four files.

160
00:07:48,810 --> 00:07:51,480
Now let's open a new Flask project

161
00:07:51,480 --> 00:07:55,290
and let me show you how you would add a gitignore file

162
00:07:55,290 --> 00:07:57,000
for your Python projects.

163
00:07:57,000 --> 00:07:59,520
So let's just call this test,

164
00:07:59,520 --> 00:08:03,360
and I'm gonna put it inside my desktop.

165
00:08:03,360 --> 00:08:04,860
So after I open the project,

166
00:08:04,860 --> 00:08:07,263
I'm going to create a new virtual environment.

167
00:08:11,040 --> 00:08:13,080
And I'm going to install all the modules

168
00:08:13,080 --> 00:08:15,243
from the requirements.txt file,

169
00:08:16,560 --> 00:08:18,330
then I'm going to open the terminal

170
00:08:18,330 --> 00:08:21,543
and create a gitignore file.

171
00:08:22,710 --> 00:08:25,077
Then we're gonna open our gitignore,

172
00:08:25,950 --> 00:08:29,790
and in here I'm going to add some of the files

173
00:08:29,790 --> 00:08:33,030
that will be ignored for our Python project.

174
00:08:33,030 --> 00:08:36,090
If you go to github.com/github,

175
00:08:36,090 --> 00:08:39,900
so this is a repository owned by the GitHub team,

176
00:08:39,900 --> 00:08:42,870
they have a repository called gitignore,

177
00:08:42,870 --> 00:08:45,900
and it's basically a pre-made collection

178
00:08:45,900 --> 00:08:48,150
of useful gitignore templates.

179
00:08:48,150 --> 00:08:50,730
And if you have a search in here,

180
00:08:50,730 --> 00:08:52,590
you can find that you've got Python,

181
00:08:52,590 --> 00:08:56,820
and you can see that this is a prebuilt template

182
00:08:56,820 --> 00:09:00,720
for some of the files that you probably won't want to upload

183
00:09:00,720 --> 00:09:03,930
to GitHub or any other remote repository.

184
00:09:03,930 --> 00:09:06,960
So these have the file extensions for things

185
00:09:06,960 --> 00:09:10,800
that are virtual environments or environment settings,

186
00:09:10,800 --> 00:09:13,590
and it's basically all pre-made for you.

187
00:09:13,590 --> 00:09:17,140
So you can simply just copy all of this

188
00:09:18,060 --> 00:09:21,510
and put it into your git ignore file

189
00:09:21,510 --> 00:09:23,370
that you created earlier on.

190
00:09:23,370 --> 00:09:25,440
So just straight up paste.

191
00:09:25,440 --> 00:09:26,730
And if you remember from earlier on,

192
00:09:26,730 --> 00:09:28,590
we said that the lines that start off

193
00:09:28,590 --> 00:09:31,410
with a pound sign are the comments,

194
00:09:31,410 --> 00:09:34,440
and these ones are extensions that you would ignore.

195
00:09:34,440 --> 00:09:35,940
And you might want to add things

196
00:09:35,940 --> 00:09:38,190
like .DS_store into here as well.

197
00:09:38,190 --> 00:09:41,310
So you can have something like Project-wide.

198
00:09:41,310 --> 00:09:45,570
So for example, the .DS_Store that we mentioned earlier on,

199
00:09:45,570 --> 00:09:49,110
and if you hit save, now, if we go into terminal,

200
00:09:49,110 --> 00:09:51,480
and we're inside our test folder,

201
00:09:51,480 --> 00:09:53,553
so we're gonna do a git init,

202
00:09:55,497 --> 00:09:56,603
git add, git status

203
00:09:59,430 --> 00:10:01,563
to see what's gonna be committed.

204
00:10:02,910 --> 00:10:07,910
And then we're gonna say git commit -m initial commit.

205
00:10:10,140 --> 00:10:14,010
There we go, and now, once you're ready to commit

206
00:10:14,010 --> 00:10:16,350
and add all of this to your remote,

207
00:10:16,350 --> 00:10:20,790
then it will ignore all of these common user related files

208
00:10:20,790 --> 00:10:24,330
that are not gonna be useful to your collaborators

209
00:10:24,330 --> 00:10:27,420
or anybody who wants to contribute on GitHub.

210
00:10:27,420 --> 00:10:31,019
That was just a quick tidbit about using gitignore,

211
00:10:31,019 --> 00:10:33,570
and I know that some of you guys have been asking

212
00:10:33,570 --> 00:10:36,630
about, you know, where should you put your API keys,

213
00:10:36,630 --> 00:10:39,120
where should you put your sensitive data?

214
00:10:39,120 --> 00:10:40,230
And you have to make sure

215
00:10:40,230 --> 00:10:42,660
that when you are uploading your projects

216
00:10:42,660 --> 00:10:45,360
with these secret keys and passwords,

217
00:10:45,360 --> 00:10:47,850
to make sure that you set up a gitignore file,

218
00:10:47,850 --> 00:10:49,893
and to include those things in there.

219
00:10:51,000 --> 00:10:53,820
All right, so that's all for this lesson.

220
00:10:53,820 --> 00:10:56,880
In the next lesson I wanna talk about Git clone,

221
00:10:56,880 --> 00:11:01,530
and how to clone various repositories to your local system.

222
00:11:01,530 --> 00:11:04,230
So all of that and more in the next lesson.

223
00:11:04,230 --> 00:11:05,330
So I'll see you there.

