1
00:00:02,110 --> 00:00:04,510
So we learned a lot about commits

2
00:00:04,510 --> 00:00:07,150
and branches now and so far,

3
00:00:07,150 --> 00:00:12,150
we only added data to our well, Git-managed project here.

4
00:00:12,560 --> 00:00:16,420
Now is the time to also delete information.

5
00:00:16,420 --> 00:00:20,460
And for this, I will leave our main branch here

6
00:00:20,460 --> 00:00:22,480
and first create a new branch.

7
00:00:22,480 --> 00:00:27,180
So we can run git checkout -b,

8
00:00:27,180 --> 00:00:28,750
and now a new branch name.

9
00:00:28,750 --> 00:00:32,310
I'll just call it delete

10
00:00:32,310 --> 00:00:35,253
because we will delete well, data in this branch.

11
00:00:36,980 --> 00:00:39,050
So we are in this branch now

12
00:00:39,050 --> 00:00:42,370
and in this branch, I'll add a new folder maybe.

13
00:00:42,370 --> 00:00:45,240
Not in here, maybe right there.

14
00:00:45,240 --> 00:00:48,290
I'm deleting-data.

15
00:00:48,290 --> 00:00:49,440
This one.

16
00:00:49,440 --> 00:00:53,040
And in there, I'll add a new file.

17
00:00:53,040 --> 00:00:54,697
So file1.txt.

18
00:00:57,880 --> 00:01:01,933
We'll then add this file and commit it.

19
00:01:04,959 --> 00:01:05,863
Added file1.txt.

20
00:01:12,220 --> 00:01:14,283
Then we'll add some dummy text in here

21
00:01:14,283 --> 00:01:16,180
just to show you how deletion works.

22
00:01:16,180 --> 00:01:18,940
For this, we need some additional commits.

23
00:01:18,940 --> 00:01:23,940
Just some text to dive deeper here.

24
00:01:26,260 --> 00:01:27,390
We'll also save this.

25
00:01:29,230 --> 00:01:33,010
Added, now it's working.

26
00:01:33,010 --> 00:01:34,800
So git add dot

27
00:01:34,800 --> 00:01:37,980
and git commit -m.

28
00:01:37,980 --> 00:01:42,283
Added text to file1.txt maybe.

29
00:01:43,640 --> 00:01:45,541
Like this.

30
00:01:45,541 --> 00:01:47,050
And let's clear this.

31
00:01:47,050 --> 00:01:49,350
And now we'll add a third commit

32
00:01:51,100 --> 00:01:55,370
where we could say some more

33
00:01:57,300 --> 00:01:59,023
text in here.

34
00:02:00,260 --> 00:02:02,963
And finally, our last commit.

35
00:02:05,310 --> 00:02:07,700
Added more text.

36
00:02:07,700 --> 00:02:10,259
Not too important here what the commit says.

37
00:02:10,259 --> 00:02:13,040
And now besides this first file here,

38
00:02:13,040 --> 00:02:15,010
I would like to add two more files.

39
00:02:15,010 --> 00:02:16,260
Let's quickly do this.

40
00:02:16,260 --> 00:02:17,093
So file2.txt.

41
00:02:19,500 --> 00:02:21,780
And also commit this.

42
00:02:21,780 --> 00:02:25,673
Commit -m.

43
00:02:26,640 --> 00:02:28,853
Added file2.txt.

44
00:02:30,020 --> 00:02:32,887
And now we'll add a third file here.

45
00:02:32,887 --> 00:02:34,443
File3.txt,

46
00:02:36,410 --> 00:02:39,483
which we'll also add as a separate commit.

47
00:02:41,080 --> 00:02:45,690
And git commit -m file3.txt.

48
00:02:45,690 --> 00:02:48,310
I know lots of commits we just created here

49
00:02:48,310 --> 00:02:50,450
but to understand how deleting works,

50
00:02:50,450 --> 00:02:53,670
well, it's great to have these examples.

51
00:02:53,670 --> 00:02:55,320
We can also clear this again

52
00:02:55,320 --> 00:02:57,430
and now log our information,

53
00:02:57,430 --> 00:03:01,763
and so we see well, we have some data to play with now.

54
00:03:02,900 --> 00:03:05,610
I would like to get started with a very well,

55
00:03:05,610 --> 00:03:07,470
simple operation actually,

56
00:03:07,470 --> 00:03:11,650
and this is deleting files from the working directory.

57
00:03:11,650 --> 00:03:14,100
As you saw, the three files we added now

58
00:03:14,100 --> 00:03:16,380
are part of previous commits.

59
00:03:16,380 --> 00:03:18,870
Therefore, these are all tracked files,

60
00:03:18,870 --> 00:03:22,113
and to delete these, well, we have two options basically.

61
00:03:23,240 --> 00:03:26,260
A very common one, of course, is to simply well,

62
00:03:26,260 --> 00:03:27,800
right click onto the file

63
00:03:29,070 --> 00:03:32,123
and press Delete, and then you can move it to the trash.

64
00:03:33,470 --> 00:03:35,863
With this, and by checking its status,

65
00:03:37,301 --> 00:03:41,310
you see that we have to add this deleted file so to say

66
00:03:41,310 --> 00:03:43,420
to the staging area

67
00:03:43,420 --> 00:03:46,040
because we have unstaged changes to be committed,

68
00:03:46,040 --> 00:03:50,580
so we have to tell Git that this file should be executed.

69
00:03:50,580 --> 00:03:53,700
Alternatively, we can also run git rm

70
00:03:53,700 --> 00:03:55,510
and now refer to the file name.

71
00:03:55,510 --> 00:03:59,773
File2.txt, like this, so git rm for remove.

72
00:04:00,800 --> 00:04:02,820
And there you see this doesn't work.

73
00:04:02,820 --> 00:04:04,730
Why's it not working?

74
00:04:04,730 --> 00:04:08,230
Well, because currently we are in the git-basics folder

75
00:04:08,230 --> 00:04:11,840
but to delete a specific file without specifying a path,

76
00:04:11,840 --> 00:04:14,280
we have to navigate into this path,

77
00:04:14,280 --> 00:04:17,320
so with cd deleting-data,

78
00:04:17,320 --> 00:04:19,579
we are in the correct path now.

79
00:04:19,579 --> 00:04:23,857
And in there, we can now run git remove file2.txt.

80
00:04:28,559 --> 00:04:32,150
With that, you see that the file was deleted.

81
00:04:32,150 --> 00:04:35,013
And by running git status once again,

82
00:04:36,580 --> 00:04:39,360
you see that we have a difference here.

83
00:04:39,360 --> 00:04:42,530
If we delete the file from the working directory

84
00:04:42,530 --> 00:04:43,660
as we did it,

85
00:04:43,660 --> 00:04:46,810
we have to add this change to the staging area first.

86
00:04:46,810 --> 00:04:47,800
You can see this down here.

87
00:04:47,800 --> 00:04:50,020
This is the deleted file with that red text,

88
00:04:50,020 --> 00:04:52,080
and these changes are not staged.

89
00:04:52,080 --> 00:04:53,330
I mentioned this already.

90
00:04:54,440 --> 00:04:57,640
Up here, when we use the git remove command,

91
00:04:57,640 --> 00:05:00,990
we see that the changes can be immediately committed.

92
00:05:00,990 --> 00:05:04,640
So with this, this means Git automatically removes the file

93
00:05:04,640 --> 00:05:07,550
from the staging area for us.

94
00:05:07,550 --> 00:05:09,830
Therefore, if you just want to delete files,

95
00:05:09,830 --> 00:05:13,400
git remove or git rm saves you the additional adding

96
00:05:13,400 --> 00:05:15,680
to the staging area step.

97
00:05:15,680 --> 00:05:18,600
Nevertheless, we can, of course, add this

98
00:05:18,600 --> 00:05:20,720
because we have to add the file we deleted

99
00:05:20,720 --> 00:05:21,880
with the right click

100
00:05:21,880 --> 00:05:26,880
and then we can commit this and say deleted file2 and file3.

101
00:05:31,580 --> 00:05:33,130
Like this.

102
00:05:33,130 --> 00:05:37,540
And with that, if we check the status,

103
00:05:37,540 --> 00:05:40,870
you see everything is cool, nothing to commit.

104
00:05:40,870 --> 00:05:42,800
We can also close this window now

105
00:05:42,800 --> 00:05:44,163
and focus onto file1.txt

106
00:05:46,570 --> 00:05:50,420
because besides deleting working directory files,

107
00:05:50,420 --> 00:05:53,890
we might also want to delete individual commits

108
00:05:53,890 --> 00:05:55,910
or to be more precise here,

109
00:05:55,910 --> 00:05:58,820
we might want to go back to an earlier stage

110
00:05:58,820 --> 00:06:00,650
of our project, and therefore,

111
00:06:00,650 --> 00:06:03,900
delete the latest commit, for example.

112
00:06:03,900 --> 00:06:06,023
If we go to our git log here,

113
00:06:07,160 --> 00:06:08,950
you see that currently our HEAD

114
00:06:08,950 --> 00:06:10,740
is here in the latest commit.

115
00:06:10,740 --> 00:06:12,210
No big surprise.

116
00:06:12,210 --> 00:06:15,870
But what if we want to revert this change now?

117
00:06:15,870 --> 00:06:17,450
What if this should be deleted

118
00:06:17,450 --> 00:06:19,870
and we say well, actually, I would like

119
00:06:19,870 --> 00:06:21,790
to have this commit back

120
00:06:21,790 --> 00:06:24,560
where we had the second and the third file?

121
00:06:24,560 --> 00:06:26,960
Well, this can be done.

122
00:06:26,960 --> 00:06:31,960
And for this, we use a command named git reset now,

123
00:06:32,420 --> 00:06:34,140
well, to reset what?

124
00:06:34,140 --> 00:06:36,160
Basically the so-called head,

125
00:06:36,160 --> 00:06:39,930
so the head is the pointer towards this latest commit

126
00:06:39,930 --> 00:06:40,800
in our branch.

127
00:06:40,800 --> 00:06:44,823
And this should now point to well, one commit earlier.

128
00:06:45,890 --> 00:06:48,670
And to make sure we don't only reset the head,

129
00:06:48,670 --> 00:06:52,470
but that we also exclude the latest commit changes

130
00:06:52,470 --> 00:06:53,840
from the staging area

131
00:06:53,840 --> 00:06:56,700
and to basically get rid of this entire commit,

132
00:06:56,700 --> 00:07:00,460
we have to add this --hard command here.

133
00:07:00,460 --> 00:07:03,630
This means it will update the so-called index

134
00:07:03,630 --> 00:07:04,883
and the path.

135
00:07:04,883 --> 00:07:07,800
I'll not dive too deep into this at this stage

136
00:07:07,800 --> 00:07:08,830
of this module

137
00:07:08,830 --> 00:07:11,060
because the important thing that you have to keep in mind

138
00:07:11,060 --> 00:07:15,710
is with adding --hard to this reset command,

139
00:07:15,710 --> 00:07:18,050
you will kind of be able to jump back

140
00:07:18,050 --> 00:07:19,393
to an earlier commit.

141
00:07:20,440 --> 00:07:21,960
And this jumping back now works

142
00:07:21,960 --> 00:07:23,880
with the so-called HEAD command.

143
00:07:23,880 --> 00:07:26,550
As I said, the HEAD is just the pointer,

144
00:07:26,550 --> 00:07:28,750
and this pointer should now, for example,

145
00:07:28,750 --> 00:07:32,020
go back to well, one commit earlier.

146
00:07:32,020 --> 00:07:33,860
This would be ~1.

147
00:07:33,860 --> 00:07:37,960
So the head goes back one commit, HEAD~1

148
00:07:37,960 --> 00:07:40,580
and --hard also make sure

149
00:07:40,580 --> 00:07:42,510
that this is a hard reset,

150
00:07:42,510 --> 00:07:45,800
so this means we update the so-called index

151
00:07:45,800 --> 00:07:48,940
and the path, and this means we simply kind

152
00:07:48,940 --> 00:07:51,730
of delete the latest commit entirely,

153
00:07:51,730 --> 00:07:53,990
so all changes applied right there

154
00:07:53,990 --> 00:07:56,160
are removed from the repository

155
00:07:56,160 --> 00:07:59,050
and also from the staging area.

156
00:07:59,050 --> 00:08:00,353
If we do this now,

157
00:08:01,710 --> 00:08:04,070
we see that well, magically,

158
00:08:04,070 --> 00:08:07,070
our file2 and file3 are back.

159
00:08:07,070 --> 00:08:09,770
And if we now check our log,

160
00:08:09,770 --> 00:08:13,460
you see that the latest commit is no longer displayed.

161
00:08:13,460 --> 00:08:17,270
Now our file3.txt here is the latest commit

162
00:08:17,270 --> 00:08:20,340
in our branch, the HEAD points to this commit,

163
00:08:20,340 --> 00:08:23,180
and also if we quit this,

164
00:08:23,180 --> 00:08:26,040
clear it and check our staging area,

165
00:08:26,040 --> 00:08:28,853
you see everything is clean, nothing to commit.

166
00:08:30,040 --> 00:08:33,390
We could also jump back multiple commits.

167
00:08:33,390 --> 00:08:38,390
This would then be git reset --hard and then HEAD.

168
00:08:39,130 --> 00:08:43,250
Well, ~1, 2, 3 and so on.

169
00:08:43,250 --> 00:08:45,270
This would also be possible.

170
00:08:45,270 --> 00:08:48,950
But generally, this is how you jump back to earlier commits

171
00:08:48,950 --> 00:08:52,840
by, at the same time, deleting the later commits you had

172
00:08:52,840 --> 00:08:53,763
in your branch.

173
00:08:54,820 --> 00:08:56,890
Now, talking about branches,

174
00:08:56,890 --> 00:08:58,390
let me delete this quickly,

175
00:08:58,390 --> 00:09:03,060
at the moment, we have well, three branches in our project.

176
00:09:03,060 --> 00:09:06,300
This delete branch is a branch we can keep for the moment

177
00:09:06,300 --> 00:09:09,580
but the feature branch is something no longer needed.

178
00:09:09,580 --> 00:09:11,170
We merged the feature branch

179
00:09:11,170 --> 00:09:15,640
into main already, therefore we can't delete this branch.

180
00:09:15,640 --> 00:09:16,610
To delete a branch,

181
00:09:16,610 --> 00:09:18,730
you have to check out another branch

182
00:09:18,730 --> 00:09:20,020
than the one you want to delete

183
00:09:20,020 --> 00:09:21,615
because otherwise you would delete

184
00:09:21,615 --> 00:09:25,120
your current position so to say.

185
00:09:25,120 --> 00:09:27,830
In our case, we are in the delete branch.

186
00:09:27,830 --> 00:09:32,830
Therefore, we can just run git branch dash capital D,

187
00:09:33,300 --> 00:09:35,900
that's important here, it must be a capital D,

188
00:09:35,900 --> 00:09:38,560
and now simply add the branch name.

189
00:09:38,560 --> 00:09:41,000
In our case, we add feature here.

190
00:09:41,000 --> 00:09:42,430
We confirm this,

191
00:09:42,430 --> 00:09:44,850
and we also get the information in Git

192
00:09:44,850 --> 00:09:47,740
that the branch named feature was deleted,

193
00:09:47,740 --> 00:09:50,070
and with git branch,

194
00:09:50,070 --> 00:09:54,300
we see that we only have two branches left now.

195
00:09:54,300 --> 00:09:56,810
So this is how we can change files

196
00:09:56,810 --> 00:09:58,280
from the working directory,

197
00:09:58,280 --> 00:10:00,550
how we can delete commits so to say,

198
00:10:00,550 --> 00:10:03,380
and how we can delete branches.

199
00:10:03,380 --> 00:10:06,880
There are only two core features you might typically use

200
00:10:06,880 --> 00:10:07,960
in your project

201
00:10:07,960 --> 00:10:11,310
and this is how we can get rid of unstaged

202
00:10:11,310 --> 00:10:12,993
and staged changes?

203
00:10:13,840 --> 00:10:17,060
Let's get started with the unstaged change here.

204
00:10:17,060 --> 00:10:20,000
I'm here in this file1.txt file

205
00:10:20,000 --> 00:10:24,800
and in there, I add well, more content so to say.

206
00:10:24,800 --> 00:10:26,060
Let's save this.

207
00:10:26,060 --> 00:10:28,220
And if I now check my git status,

208
00:10:28,220 --> 00:10:30,030
then no big surprise,

209
00:10:30,030 --> 00:10:32,980
I have changes not staged for commit,

210
00:10:32,980 --> 00:10:35,700
so unstaged changes.

211
00:10:35,700 --> 00:10:37,810
What if you want to revert these?

212
00:10:37,810 --> 00:10:41,810
Then you simply check out the latest committed stage

213
00:10:41,810 --> 00:10:43,550
of your project so to say,

214
00:10:43,550 --> 00:10:45,020
so the situation you had

215
00:10:45,020 --> 00:10:48,240
when checking out this branch.

216
00:10:48,240 --> 00:10:52,853
This works with git checkout -- and then a dot.

217
00:10:55,710 --> 00:10:59,590
This will simply reset your branch so to say

218
00:10:59,590 --> 00:11:01,600
to the stage of the latest commit.

219
00:11:01,600 --> 00:11:04,453
If you do that, you see this text is gone.

220
00:11:05,320 --> 00:11:07,046
By the way, in case you're wondering,

221
00:11:07,046 --> 00:11:12,046
what happens if I add some text here once again?

222
00:11:16,000 --> 00:11:19,540
And then say okay, I can just check out the main branch

223
00:11:19,540 --> 00:11:22,170
and then jump back to the delete branch maybe

224
00:11:22,170 --> 00:11:23,870
to get rid of this text

225
00:11:23,870 --> 00:11:27,650
and to have access to the latest commit in that branch.

226
00:11:27,650 --> 00:11:32,360
Do this with git checkout main, for example.

227
00:11:32,360 --> 00:11:33,950
But if you try this,

228
00:11:33,950 --> 00:11:35,620
you see that this is not working.

229
00:11:35,620 --> 00:11:39,260
We have to commit our changes or stash them.

230
00:11:39,260 --> 00:11:41,260
Stashing is a bit more advanced

231
00:11:41,260 --> 00:11:45,030
and therefore not the core focus of this course here.

232
00:11:45,030 --> 00:11:46,700
Therefore, for us, this means,

233
00:11:46,700 --> 00:11:51,700
we can again run git checkout --. to get rid of this text

234
00:11:52,200 --> 00:11:54,373
as we just learned it a few seconds ago.

235
00:11:56,400 --> 00:12:00,200
So these unstaged changes and how to revert these.

236
00:12:00,200 --> 00:12:03,550
What about staged changes now as a final step

237
00:12:03,550 --> 00:12:05,280
in this lecture?

238
00:12:05,280 --> 00:12:09,750
Well, for this, I'll add some changes

239
00:12:09,750 --> 00:12:11,973
to be staged so to say.

240
00:12:12,860 --> 00:12:14,270
Saving it.

241
00:12:14,270 --> 00:12:16,880
Now I'll add it to the staging area.

242
00:12:16,880 --> 00:12:20,560
I just add all changes with git add dot again.

243
00:12:20,560 --> 00:12:23,690
And now I say well, actually I see

244
00:12:23,690 --> 00:12:26,710
that these changes are ready in the staging area

245
00:12:26,710 --> 00:12:29,430
but actually, I don't want these changes to be part

246
00:12:29,430 --> 00:12:31,250
of my next commit.

247
00:12:31,250 --> 00:12:35,060
You could use this git restore command for this case here.

248
00:12:35,060 --> 00:12:36,480
That's one approach.

249
00:12:36,480 --> 00:12:39,480
I'll show you an alternative one though.

250
00:12:39,480 --> 00:12:42,053
And this is simply git reset,

251
00:12:43,600 --> 00:12:45,910
and now we refer to the file name,

252
00:12:45,910 --> 00:12:49,160
so file1.txt in our case.

253
00:12:49,160 --> 00:12:51,010
This is step one.

254
00:12:51,010 --> 00:12:53,393
With this, if we now check our status,

255
00:12:54,850 --> 00:12:58,280
you see that now we have changes not staged for commit,

256
00:12:58,280 --> 00:13:01,000
so this is basically the same logic

257
00:13:01,000 --> 00:13:05,300
that we had before when undoing unstaged changes.

258
00:13:05,300 --> 00:13:09,113
So in this case, we can simply run again git checkout --.

259
00:13:10,770 --> 00:13:14,280
to undo all unstaged changes.

260
00:13:14,280 --> 00:13:16,760
Or we simply reset the individual file,

261
00:13:16,760 --> 00:13:20,248
so this would be file1.txt in our case,

262
00:13:20,248 --> 00:13:23,310
and with this in place, you see that the text is deleted,

263
00:13:23,310 --> 00:13:24,930
and if we clear this now,

264
00:13:24,930 --> 00:13:28,350
and check the status, you see everything is clear,

265
00:13:28,350 --> 00:13:29,473
nothing to commit.

266
00:13:30,900 --> 00:13:35,260
With this, I also want to conclude this Git part

267
00:13:35,260 --> 00:13:36,540
of this module.

268
00:13:36,540 --> 00:13:40,520
So far, we only worked locally on our project

269
00:13:40,520 --> 00:13:43,320
but there is more to explore in the Git world,

270
00:13:43,320 --> 00:13:47,630
and to explore more, we have to leave our local environment

271
00:13:47,630 --> 00:13:50,703
and dive into the world of GitHub now.

