1
00:00:00,270 --> 00:00:01,740
Instructor: Now in the last lesson,

2
00:00:01,740 --> 00:00:03,810
we spoke about some of the more basic variants

3
00:00:03,810 --> 00:00:05,400
of version control.

4
00:00:05,400 --> 00:00:09,450
In this lesson, I wanna talk about branches and branching.

5
00:00:09,450 --> 00:00:11,610
Now let's start off with a simple example.

6
00:00:11,610 --> 00:00:15,090
Say if we had version one and two,

7
00:00:15,090 --> 00:00:19,020
so two commits that were made to our local repository.

8
00:00:19,020 --> 00:00:21,570
And at this point, we realize that

9
00:00:21,570 --> 00:00:23,730
we want to maybe try out something different,

10
00:00:23,730 --> 00:00:25,740
maybe build a new feature

11
00:00:25,740 --> 00:00:29,040
or just to mess around with a new idea or concept.

12
00:00:29,040 --> 00:00:32,189
What we can do is instead of continuing to commit

13
00:00:32,189 --> 00:00:34,740
to the main branch that you see here,

14
00:00:34,740 --> 00:00:37,950
we can also create a side branch.

15
00:00:37,950 --> 00:00:41,790
After the second commit, we create a new branch

16
00:00:41,790 --> 00:00:44,910
and we start committing to this new branch

17
00:00:44,910 --> 00:00:47,160
or this experimental branch.

18
00:00:47,160 --> 00:00:50,400
We add some features and we write some code.

19
00:00:50,400 --> 00:00:54,210
Simultaneously, we can continue working on the main branch,

20
00:00:54,210 --> 00:00:56,910
putting out all those essential updates

21
00:00:56,910 --> 00:01:01,910
or bits of code that are maintaining our main project.

22
00:01:01,950 --> 00:01:05,099
But at the same time, we can continue to update

23
00:01:05,099 --> 00:01:07,590
and work on this experimental branch,

24
00:01:07,590 --> 00:01:10,080
trying things out and committing our experiments

25
00:01:10,080 --> 00:01:11,970
to this experimental branch.

26
00:01:11,970 --> 00:01:16,020
So now we have two branches that are parallel to each other

27
00:01:16,020 --> 00:01:18,750
and they can be developed simultaneously.

28
00:01:18,750 --> 00:01:20,550
If at some point in the future

29
00:01:20,550 --> 00:01:23,970
that we decide that that experiment was really fruitful

30
00:01:23,970 --> 00:01:27,390
and the feature that we built in it was really, really great

31
00:01:27,390 --> 00:01:30,750
and we'd like to merge it back to the main branch

32
00:01:30,750 --> 00:01:32,460
or to the main project,

33
00:01:32,460 --> 00:01:34,770
then that can be done really easily as well

34
00:01:34,770 --> 00:01:37,980
by simply placing a merge request in.

35
00:01:37,980 --> 00:01:40,410
And we can bring all of those changes

36
00:01:40,410 --> 00:01:43,650
that we experimented with, that we messed around with,

37
00:01:43,650 --> 00:01:45,480
back to the main project

38
00:01:45,480 --> 00:01:47,640
and check to see if there's any conflict

39
00:01:47,640 --> 00:01:49,320
with the main branch code.

40
00:01:49,320 --> 00:01:52,020
And if not, or if after a little bit of editing,

41
00:01:52,020 --> 00:01:54,630
then we can bring all of those changes

42
00:01:54,630 --> 00:01:57,273
into the main working branch.

43
00:01:58,260 --> 00:02:00,690
And then we can continue working from here

44
00:02:00,690 --> 00:02:04,770
onto the next commit or we can make more branches.

45
00:02:04,770 --> 00:02:07,020
And very often what you see in practice

46
00:02:07,020 --> 00:02:10,350
is that there'll be multiple branches being worked on

47
00:02:10,350 --> 00:02:13,590
at the same time for any given large project.

48
00:02:13,590 --> 00:02:15,000
And the reason is because

49
00:02:15,000 --> 00:02:17,040
sometimes you're developing new features,

50
00:02:17,040 --> 00:02:19,500
sometimes you are fixing bugs,

51
00:02:19,500 --> 00:02:22,980
and all of these things may break your main project.

52
00:02:22,980 --> 00:02:25,440
So you don't want to do it on the main branch.

53
00:02:25,440 --> 00:02:28,230
You only want to put it on to the main branch

54
00:02:28,230 --> 00:02:31,650
once you know that everything is working fine

55
00:02:31,650 --> 00:02:35,130
and then you can bring your code back to the working copy

56
00:02:35,130 --> 00:02:37,710
ready for shipment and deployment.

57
00:02:37,710 --> 00:02:41,640
Let's take a look at how this would work in reality.

58
00:02:41,640 --> 00:02:46,083
Now let's say we navigate back to our story directory.

59
00:02:47,160 --> 00:02:51,390
And inside here, we've still got our previous three chapters

60
00:02:51,390 --> 00:02:53,370
and they are under version control.

61
00:02:53,370 --> 00:02:55,500
So if we just have a look at git log,

62
00:02:55,500 --> 00:02:59,550
you can see that this is currently the most recent commit

63
00:02:59,550 --> 00:03:02,463
and it's also mirrored in our remote.

64
00:03:03,720 --> 00:03:06,750
Let's try and do this locally first.

65
00:03:06,750 --> 00:03:10,020
If I decided that I want to create a new branch,

66
00:03:10,020 --> 00:03:13,260
you can simply just write git branch

67
00:03:13,260 --> 00:03:16,230
and specify the name of your new branch.

68
00:03:16,230 --> 00:03:20,880
I'm going to add a space related plot to my story

69
00:03:20,880 --> 00:03:25,653
so let's call our branch alien-plot.

70
00:03:27,210 --> 00:03:28,080
Hit Enter.

71
00:03:28,080 --> 00:03:30,600
And now you can check out what branches you have

72
00:03:30,600 --> 00:03:34,530
by just writing git branch without the name.

73
00:03:34,530 --> 00:03:37,950
And you can see that you've got one branch called alien-plot

74
00:03:37,950 --> 00:03:39,270
and another one called main

75
00:03:39,270 --> 00:03:41,940
and the asterisk shows you which branch

76
00:03:41,940 --> 00:03:43,620
you are currently on.

77
00:03:43,620 --> 00:03:45,600
So we're currently on the main branch.

78
00:03:45,600 --> 00:03:48,330
We can switch to the alien-plot

79
00:03:48,330 --> 00:03:53,130
by simply writing git checkout alien-plot.

80
00:03:56,730 --> 00:03:57,900
Okay, as it says,

81
00:03:57,900 --> 00:04:01,680
we have now switched to the branch called alien-plot.

82
00:04:01,680 --> 00:04:03,300
Now it is inside this branch

83
00:04:03,300 --> 00:04:07,830
that I'm going to make some changes to my chapters.

84
00:04:07,830 --> 00:04:12,720
Now I'm gonna make some completely nonsensical modification

85
00:04:12,720 --> 00:04:14,850
to my files.

86
00:04:14,850 --> 00:04:16,769
So let's change chapter one.

87
00:04:16,769 --> 00:04:19,773
Let's change chapter two.

88
00:04:26,640 --> 00:04:28,650
Okay, there we go.

89
00:04:28,650 --> 00:04:31,110
Okay, so we've made some modifications to chapter one,

90
00:04:31,110 --> 00:04:32,040
chapter two

91
00:04:32,040 --> 00:04:34,590
and we've decided to change

92
00:04:34,590 --> 00:04:37,050
some of the pre-existing written text

93
00:04:37,050 --> 00:04:40,983
to integrate a space-themed or alien-themed plot.

94
00:04:42,240 --> 00:04:44,280
Let's go ahead and commit that.

95
00:04:44,280 --> 00:04:46,540
So remember we have to do git add

96
00:04:47,550 --> 00:04:51,480
and then git commit -m

97
00:04:51,480 --> 00:04:56,330
and our message is going to be modify chapter one

98
00:04:59,790 --> 00:05:04,493
and two to have alien theme.

99
00:05:06,570 --> 00:05:11,250
Okay, now hit Enter and we've made our commits.

100
00:05:11,250 --> 00:05:13,650
Now if we do a git log,

101
00:05:13,650 --> 00:05:17,580
we can see that we have two commits made on the main branch

102
00:05:17,580 --> 00:05:22,440
and we have one commit made on the alien-plot branch.

103
00:05:22,440 --> 00:05:26,190
So now let's say that we go back onto our main branch.

104
00:05:26,190 --> 00:05:28,290
Whenever you're confused where you are,

105
00:05:28,290 --> 00:05:30,600
you can always do a git branch to check

106
00:05:30,600 --> 00:05:32,550
and see where the asterisk is

107
00:05:32,550 --> 00:05:35,520
and the asterisk is obviously where you are.

108
00:05:35,520 --> 00:05:39,030
So let's do a git checkout main

109
00:05:39,030 --> 00:05:41,640
to go back to the main branch.

110
00:05:41,640 --> 00:05:44,970
So you can see that our main branch is unchanged

111
00:05:44,970 --> 00:05:47,700
with respect to the space or alien plot

112
00:05:47,700 --> 00:05:48,960
that we did just now.

113
00:05:48,960 --> 00:05:51,330
Nothing has changed over here.

114
00:05:51,330 --> 00:05:54,840
So while on the main branch, I'm going to create a new file.

115
00:05:54,840 --> 00:05:56,677
I'm gonna call it chapter4.txt.

116
00:06:01,200 --> 00:06:05,283
And inside chapter four, let's add something.

117
00:06:09,780 --> 00:06:12,030
Okay and hit Enter and say...

118
00:06:12,030 --> 00:06:13,680
By the way, I have no idea what I'm typing.

119
00:06:13,680 --> 00:06:15,750
I'm now making up and destroying

120
00:06:15,750 --> 00:06:18,810
probably in the process a masterpiece but it's okay.

121
00:06:18,810 --> 00:06:21,390
So we now have a chapter four on our main branch

122
00:06:21,390 --> 00:06:26,103
and I'm gonna go ahead and do a git add and git commit.

123
00:06:27,090 --> 00:06:29,880
Now there's ways of combining add and commit together

124
00:06:29,880 --> 00:06:31,410
in the same command

125
00:06:31,410 --> 00:06:32,730
but I think if you're new to Git,

126
00:06:32,730 --> 00:06:34,920
it's always good to really know in your head

127
00:06:34,920 --> 00:06:36,270
exactly what's going on.

128
00:06:36,270 --> 00:06:39,660
So I recommend actually separating those two bits out.

129
00:06:39,660 --> 00:06:42,870
Let's go ahead and give it a commit message

130
00:06:42,870 --> 00:06:45,123
and chapter four.

131
00:06:46,050 --> 00:06:48,000
Okay, so git log.

132
00:06:48,000 --> 00:06:49,590
Let's see, what have we got?

133
00:06:49,590 --> 00:06:52,020
We are currently only looking at the main branch

134
00:06:52,020 --> 00:06:53,790
and we have three commits.

135
00:06:53,790 --> 00:06:57,480
This is the position of our remote.

136
00:06:57,480 --> 00:07:00,030
So on our GitHub repository,

137
00:07:00,030 --> 00:07:02,910
this was the latest commit that it could see.

138
00:07:02,910 --> 00:07:05,460
But on our local Git repository,

139
00:07:05,460 --> 00:07:07,260
this is in fact the latest commit,

140
00:07:07,260 --> 00:07:10,050
the one that we just made where we added chapter four.

141
00:07:10,050 --> 00:07:12,750
And you can see that if I switch between the branches,

142
00:07:12,750 --> 00:07:17,310
say if I go over to alien-plot branch and hit Enter,

143
00:07:17,310 --> 00:07:20,640
you can see that our local files actually changed

144
00:07:20,640 --> 00:07:22,230
as I switch branches.

145
00:07:22,230 --> 00:07:25,470
Let's just say that I'm quite happy with the changes

146
00:07:25,470 --> 00:07:29,250
that I've made in terms of my alien-plot addition

147
00:07:29,250 --> 00:07:32,400
and I would like to merge these changes

148
00:07:32,400 --> 00:07:33,870
back into my main branch.

149
00:07:33,870 --> 00:07:36,210
So I've done a little bit of experimentation

150
00:07:36,210 --> 00:07:37,560
on a separate branch.

151
00:07:37,560 --> 00:07:39,990
I've, you know, messed with a few things

152
00:07:39,990 --> 00:07:42,150
and I think it was a great experiment

153
00:07:42,150 --> 00:07:46,380
so I'm going to put it back into our main branch.

154
00:07:46,380 --> 00:07:47,580
In order to do that,

155
00:07:47,580 --> 00:07:50,820
what you have to do is go back to the main branch.

156
00:07:50,820 --> 00:07:53,640
So git checkout main

157
00:07:53,640 --> 00:07:55,500
and while we're on the main branch,

158
00:07:55,500 --> 00:07:58,110
we're going to merge the changes

159
00:07:58,110 --> 00:08:00,360
inside the alien-plot branch.

160
00:08:00,360 --> 00:08:03,070
So we use the command git merge

161
00:08:04,080 --> 00:08:07,050
and we're going to specify the branch name

162
00:08:07,050 --> 00:08:09,600
which was alien-plot.

163
00:08:09,600 --> 00:08:10,590
Hit Enter.

164
00:08:10,590 --> 00:08:13,590
And this opens up VIM which is a text editor.

165
00:08:13,590 --> 00:08:16,710
And this allows you to add a merge message if you wish.

166
00:08:16,710 --> 00:08:18,990
And alternatively, as we're gonna do here

167
00:08:18,990 --> 00:08:20,490
which is gonna leave it empty,

168
00:08:20,490 --> 00:08:24,423
and you're gonna write :q! to save and quit.

169
00:08:25,590 --> 00:08:29,940
And you can see now I've actually absorbed those changes

170
00:08:29,940 --> 00:08:31,860
from the alien-plot.

171
00:08:31,860 --> 00:08:36,510
And you can see that because if I do git branch,

172
00:08:36,510 --> 00:08:37,647
I am on the main branch.

173
00:08:37,647 --> 00:08:40,740
The alien-plot branch still exists by the way.

174
00:08:40,740 --> 00:08:42,539
If you have a look at git log,

175
00:08:42,539 --> 00:08:45,750
you can see that I've got some of the previous commits

176
00:08:45,750 --> 00:08:48,630
but I've also got this merge branch alien-plot

177
00:08:48,630 --> 00:08:51,330
which was my most recent commit.

178
00:08:51,330 --> 00:08:56,330
Now at this point, let's do a git push

179
00:08:56,460 --> 00:08:58,560
to our origin domain

180
00:08:58,560 --> 00:09:00,933
and remember to add the -U flag.

181
00:09:02,280 --> 00:09:05,310
That's completed and let's check it out online.

182
00:09:05,310 --> 00:09:07,890
If we go over to our story repository,

183
00:09:07,890 --> 00:09:11,280
you can see that there's now five commits.

184
00:09:11,280 --> 00:09:13,080
We modify chapter one and two,

185
00:09:13,080 --> 00:09:14,580
we added chapter four

186
00:09:14,580 --> 00:09:17,400
and we merge the alien-plot branch.

187
00:09:17,400 --> 00:09:20,550
So if you go into Insights and go to Network.

188
00:09:20,550 --> 00:09:23,850
Now if I zoom in on this network graph,

189
00:09:23,850 --> 00:09:26,520
then you can see this is the process

190
00:09:26,520 --> 00:09:27,990
that we've gone through.

191
00:09:27,990 --> 00:09:31,020
This is the main branch where we did chapter one

192
00:09:31,020 --> 00:09:32,400
and chapter two.

193
00:09:32,400 --> 00:09:34,200
And then at this point

194
00:09:34,200 --> 00:09:36,540
after I created chapter two and three,

195
00:09:36,540 --> 00:09:39,390
I created a new branch.

196
00:09:39,390 --> 00:09:41,640
And while inside this branch, I made a commit

197
00:09:41,640 --> 00:09:43,680
that modified chapter one and two

198
00:09:43,680 --> 00:09:45,720
to have an alien theme.

199
00:09:45,720 --> 00:09:47,580
And then on the main branch,

200
00:09:47,580 --> 00:09:49,680
I continued developing the main branch

201
00:09:49,680 --> 00:09:51,660
adding a chapter four.

202
00:09:51,660 --> 00:09:56,190
But subsequently, I realized that I did a great job

203
00:09:56,190 --> 00:09:59,730
adding alien themes to our storyline

204
00:09:59,730 --> 00:10:04,530
and I decided to merge it back into the main branch.

205
00:10:04,530 --> 00:10:07,380
This is basically a graphical representation

206
00:10:07,380 --> 00:10:09,180
of what's been going on.

207
00:10:09,180 --> 00:10:12,840
So that was us creating a branch and merging it.

208
00:10:12,840 --> 00:10:14,790
Now why don't you go ahead and have a go

209
00:10:14,790 --> 00:10:17,070
and create your own repository

210
00:10:17,070 --> 00:10:19,740
both locally as well as on GitHub

211
00:10:19,740 --> 00:10:23,130
and you know, write a story or write a poem,

212
00:10:23,130 --> 00:10:24,270
anything you like

213
00:10:24,270 --> 00:10:29,270
and check out the amazingness that is branching using Git.

