0
1
00:00:00,360 --> 00:00:05,520
Now, in the last lesson, we spoke about some of the more basic variants of version control.
1

2
00:00:05,520 --> 00:00:09,600
Now, in this lesson, I want to talk about branches and branching.
2

3
00:00:09,600 --> 00:00:15,590
Now, let's start off with a simple example, say, if we had version 1 and 2,
3

4
00:00:15,630 --> 00:00:19,590
so two commits that were made to our local repository.
4

5
00:00:19,590 --> 00:00:26,010
Now, at this point, we realize that we want to maybe try out something different, maybe build a new feature,
5

6
00:00:26,180 --> 00:00:29,410
or just to mess around with a new idea or concept.
6

7
00:00:29,430 --> 00:00:36,210
So what we can do is, instead of continuing to commit to the master branch, which is the main branch that
7

8
00:00:36,210 --> 00:00:43,110
you see here, we can also create a side branch so we can create as many branches as we like.
8

9
00:00:43,260 --> 00:00:51,330
So after the second commit, we create a new branch and we start committing to this new branch or this
9

10
00:00:51,390 --> 00:00:56,210
experimental branch. We add some features and we write some code.
10

11
00:00:56,250 --> 00:01:02,910
Now, simultaneously, we can continue working on the main branch, putting out all those essential updates
11

12
00:01:02,970 --> 00:01:11,100
or bits of code that are maintaining our main project. But at the same time, we can continue to update
12

13
00:01:11,190 --> 00:01:17,550
and work on this experimental branch trying things out and committing our experiment to this experimental
13

14
00:01:17,550 --> 00:01:18,150
branch.
14

15
00:01:18,330 --> 00:01:24,800
So, now we have two branches that are parallel to each other and they can be developed simultaneously.
15

16
00:01:25,050 --> 00:01:31,440
Now, if at some point in the future that we decide that that experiment was really fruitful and the feature
16

17
00:01:31,440 --> 00:01:37,530
that we built in it was really, really great and we'd like to merge it back to the main branch or to
17

18
00:01:37,530 --> 00:01:44,390
the main project, then that can be done really easily as well by simply placing a merge request in.
18

19
00:01:44,550 --> 00:01:51,030
And we can bring all of those changes that we experimented with, that we messed around with, back to the
19

20
00:01:51,030 --> 00:01:55,910
main project and check to see if there's any conflicts with the main branch code,
20

21
00:01:55,920 --> 00:02:03,090
and if not, or if after a little bit editing, then we can bring all of those changes into the main working
21

22
00:02:03,330 --> 00:02:11,460
master branch. And then we can continue working from here onto the next commit or we can make more branches.
22

23
00:02:11,970 --> 00:02:17,700
And very often what you see in practice is that there'll be multiple branches being worked on at the
23

24
00:02:17,700 --> 00:02:20,470
same time for any given large project.
24

25
00:02:20,730 --> 00:02:26,840
And the reason is because sometimes you're developing new features, sometimes you are fixing bugs,
25

26
00:02:27,120 --> 00:02:33,140
and all of these things may break your main project, so you don't want to do it on the master branch.
26

27
00:02:33,210 --> 00:02:40,050
You only want to put it on the master branch once you've tested, once you know that everything is working
27

28
00:02:40,050 --> 00:02:46,580
fine, and then you can bring your code back to the working copy ready for shipment and deployment.
28

29
00:02:46,920 --> 00:02:51,080
So let's take a look at how this would work in reality.
29

30
00:02:51,090 --> 00:02:55,350
Now, let's say, we navigate back to our Story directory.
30

31
00:02:57,900 --> 00:03:04,100
And inside here, we've still got our previous three chapters and they are under version control.
31

32
00:03:04,110 --> 00:03:10,740
So if we just have to look at git log, you can see that this is currently the most recent commit and it's
32

33
00:03:10,800 --> 00:03:13,780
also mirrored in our remotes.
33

34
00:03:14,520 --> 00:03:18,460
So let's try and do this locally first.
34

35
00:03:18,780 --> 00:03:26,130
If I decided I want to create a new branch, you can simply just write and get branch and specify the
35

36
00:03:26,130 --> 00:03:28,150
name of your new branch.
36

37
00:03:28,170 --> 00:03:33,430
So I'm going to add a space related plot to my story.
37

38
00:03:33,570 --> 00:03:39,110
So let's call our branch alien-plot.
38

39
00:03:40,620 --> 00:03:47,460
Hit enter. And now you can check out what branches you have by just writing git branch without the name
39

40
00:03:47,940 --> 00:03:54,270
and you can see that you've got one brand called alien-plot and another one called master, and the asterisk
40

41
00:03:54,390 --> 00:03:57,450
shows you which branch you are currently on.
41

42
00:03:57,450 --> 00:03:59,490
So we're currently on the master branch.
42

43
00:03:59,610 --> 00:04:07,760
We can switch to the alien-plot by simply writing "git checkout alien-plot."
43

44
00:04:10,870 --> 00:04:11,160
Okay.
44

45
00:04:11,200 --> 00:04:16,250
So as it says, we have now switched to the branch called alien-plot.
45

46
00:04:16,270 --> 00:04:24,770
Now, it is inside this branch that I'm going to make some changes to my chapters. So let's try doing that.
46

47
00:04:24,850 --> 00:04:32,830
Now, I'm going to make some completely nonsensical hum modification to my files.
47

48
00:04:32,890 --> 00:04:33,730
So let's change
48

49
00:04:33,730 --> 00:04:38,150
chapter1. Let's change chapter2.
49

50
00:04:44,640 --> 00:04:46,960
Okay. There we go. Okay.
50

51
00:04:46,960 --> 00:04:48,460
So we've made some modifications,
51

52
00:04:48,460 --> 00:04:56,050
chapter1, chapter2, and we've decided to change some of the preexisting written text to integrate a
52

53
00:04:56,050 --> 00:04:59,160
space themed or alien themed plot.
53

54
00:04:59,500 --> 00:05:02,680
So let's go ahead and commit that.
54

55
00:05:02,680 --> 00:05:09,720
So remember, we have to do git add, and then git commit -m.
55

56
00:05:09,880 --> 00:05:24,690
And our message is going to be "modify chapter 1 and 2 to have alien theme."
56

57
00:05:25,080 --> 00:05:29,400
Okay, now hit enter and we've made our commits.
57

58
00:05:29,710 --> 00:05:37,300
So, now if we do a git log, we can see that we have two commits made on the master branch and we have
58

59
00:05:37,360 --> 00:05:41,320
one commit made on the alien-plot branch.
59

60
00:05:41,350 --> 00:05:44,890
So, now let's say that we go back onto our master brunch.
60

61
00:05:44,980 --> 00:05:50,980
So whenever you're confused where you are, you can always do a git branch to check and see where the
61

62
00:05:50,980 --> 00:05:54,740
asterisk is, and the asterisk is, obviously, where you are.
62

63
00:05:54,970 --> 00:06:04,660
So let's do a git checkout master to go back to the master branch. So you can see that our master branch
63

64
00:06:04,810 --> 00:06:10,190
is unchanged with respect to the space or alien-plot that we did just now.
64

65
00:06:10,210 --> 00:06:12,380
Nothing has changed over here.
65

66
00:06:12,580 --> 00:06:16,150
So while on the master branch, I'm going to create a new file.
66

67
00:06:16,160 --> 00:06:24,760
I'm going to call that chapter4.txt. And inside chapter4,
67

68
00:06:25,220 --> 00:06:28,230
let's let's add something.
68

69
00:06:32,930 --> 00:06:36,820
Okay, and hit enter and say-- By the way, I have no idea what I'm typing.
69

70
00:06:36,890 --> 00:06:41,200
I'm now making up and destroying probably in the process a masterpiece.
70

71
00:06:41,390 --> 00:06:42,300
But it's okay.
71

72
00:06:42,530 --> 00:06:48,740
So we now have a chapter4 on our master branch and I'm going to go ahead and do a git add and
72

73
00:06:49,070 --> 00:06:50,400
git commit.
73

74
00:06:50,970 --> 00:06:55,180
Now, there's ways of combining add and commit together in the same command.
74

75
00:06:55,220 --> 00:06:56,510
But I think if you're new to git,
75

76
00:06:56,540 --> 00:06:59,980
it's always good to really know in your head exactly what's going on.
76

77
00:07:00,110 --> 00:07:03,320
So I recommend actually separating those two gits out.
77

78
00:07:03,320 --> 00:07:09,600
So let's go ahead and give it a commit message "add chapter 4."
78

79
00:07:10,160 --> 00:07:11,870
Okay, so git log.
79

80
00:07:12,080 --> 00:07:13,340
Let's see what have we got.
80

81
00:07:13,580 --> 00:07:18,250
So we are currently only looking at the master branch and we have three commits.
81

82
00:07:18,320 --> 00:07:22,010
This is the position of our remote.
82

83
00:07:22,010 --> 00:07:27,270
So on our GitHub repository, this was the latest commit that it could see.
83

84
00:07:27,410 --> 00:07:33,470
But on a local Git repository, this is in fact the latest commit, the one that we just made where we added
84

85
00:07:33,490 --> 00:07:34,400
chapter 4.
85

86
00:07:34,670 --> 00:07:41,150
And you can see that if I switch between the branches, say, if I go over to alien-plot branch and hit
86

87
00:07:41,180 --> 00:07:46,820
enter, you can see there are local files actually changed as I switched branches.
87

88
00:07:46,820 --> 00:07:53,600
So let's just say that I'm quite happy with the changes I've made in terms of my alien-plot addition,
88

89
00:07:54,170 --> 00:07:59,360
and I would like to merge these changes back into my master branch.
89

90
00:07:59,360 --> 00:08:02,910
So I've done a little bit of experimentation on a separate branch.
90

91
00:08:03,080 --> 00:08:05,480
I've messed with a few things.
91

92
00:08:05,480 --> 00:08:07,560
And I think it was a great experiment.
92

93
00:08:07,670 --> 00:08:12,290
So I'm going to put it back into our main master branch.
93

94
00:08:12,290 --> 00:08:20,280
So in order to do that, what you have to do is go back to the master branch, so "git checkout master."
94

95
00:08:20,810 --> 00:08:28,070
And while we're on the master branch, we're going to merge the changes inside the alien-plot branch.
95

96
00:08:28,070 --> 00:08:37,100
So we use the command git merge and we're going to specify the branch name which was alien-plot.
96

97
00:08:37,310 --> 00:08:38,150
Hit enter.
97

98
00:08:38,270 --> 00:08:44,220
And this opens up vim which is a text editor and this allows you to add a merge message if you wish.
98

99
00:08:44,360 --> 00:08:49,400
And alternatively, as we're going to do here which is going to leave it empty, and you're going to write 
99

100
00:08:49,490 --> 00:08:56,960
":q!" to save and quit. And you can see now I've actually absorbed those changes from the
100

101
00:08:56,990 --> 00:08:58,030
alien-plot.
101

102
00:08:58,160 --> 00:09:06,530
So I'm on my master branch. And you can see that because if I do git branch, I am on the master branch.
102

103
00:09:06,560 --> 00:09:09,150
So the alien-plot still exists by the way.
103

104
00:09:09,260 --> 00:09:14,630
So on the master branch, if you have a look at git log, you can see that I've got some of the previous
104

105
00:09:15,050 --> 00:09:21,590
commits. But I've also got this merge branch alien-plot which was my most recent commit.
105

106
00:09:21,590 --> 00:09:32,350
So, now at this point, let's do a git push to our origin master and remember to add the -u flag.
106

107
00:09:32,600 --> 00:09:33,830
So that's completed,
107

108
00:09:33,890 --> 00:09:35,780
and let's check it out online.
108

109
00:09:35,930 --> 00:09:41,980
So if we go over to our Story repository, you can see that there's now five commits.
109

110
00:09:41,990 --> 00:09:48,570
So we modify chapter 1 and 2, we added up to 4, and we merged the alien-plot branch.
110

111
00:09:48,740 --> 00:09:52,470
So if you're going to Insights, Graphs, and go to Network,
111

112
00:09:52,490 --> 00:10:00,950
now if I zoom in on this network graph and get rid of the tag, then you can see this is the process that
112

113
00:10:00,950 --> 00:10:02,070
we've gone through.
113

114
00:10:02,390 --> 00:10:06,980
So this is the master branch where we did chapter 1 and chapter 2.
114

115
00:10:07,340 --> 00:10:15,350
And then at this point, after I created chapter 2 and 3, I created a new branch. And while inside this
115

116
00:10:15,350 --> 00:10:20,490
branch, I made a commit that modified chapter 1 and 2 to have an alien theme.
116

117
00:10:20,690 --> 00:10:26,520
And then on the master branch, I continued developing the master branch adding a chapter 4.
117

118
00:10:26,810 --> 00:10:36,230
But subsequently, I realized that I did a great job of adding alien themes to our storyline and I decided
118

119
00:10:36,230 --> 00:10:39,980
to merge it back into the main branch.
119

120
00:10:40,040 --> 00:10:45,950
So this is basically a graphical representation of what's been going on. Now you can do everything I
120

121
00:10:45,950 --> 00:10:51,220
just did inside GitHub itself. And it's actually really easy to do.
121

122
00:10:51,290 --> 00:10:58,400
So let's go ahead and create a brand-new repository and I'm going to call this one Story2 because
122

123
00:10:58,430 --> 00:11:03,930
that's how imaginative I am, clearly not suited for story writing.
123

124
00:11:03,950 --> 00:11:04,900
Now, it's really important.
124

125
00:11:04,910 --> 00:11:10,870
We're going to leave our repository as public, but you're going to check this box to initialize your repository
125

126
00:11:10,880 --> 00:11:12,340
with a README.
126

127
00:11:12,470 --> 00:11:19,730
Now, README is simply a text file that tells other people what your GitHub repository is all about.
127

128
00:11:19,730 --> 00:11:28,040
So if you gone to any repository, let's just, I don't know, search for something like--
128

129
00:11:28,230 --> 00:11:29,940
And we go over here.
129

130
00:11:30,000 --> 00:11:36,350
You can see that this part that comes below the list of files is the README. And some people format
130

131
00:11:36,390 --> 00:11:44,040
this really heavily so that you can show various flags. But you can also tell people how to use your
131

132
00:11:44,460 --> 00:11:47,520
library, how to incorporate it into their project,
132

133
00:11:47,520 --> 00:11:49,190
basically, a whole bunch of useful stuff,
133

134
00:11:49,200 --> 00:11:51,950
anybody who is looking at your repository.
134

135
00:11:52,320 --> 00:11:58,260
So, now we've got our new repository. So you can click on the README file and you can make some edits
135

136
00:11:58,260 --> 00:11:59,020
if you wish.
136

137
00:11:59,160 --> 00:12:04,710
But what we're going to do is we're going to go into our main story to repository, and I'm just going
137

138
00:12:04,710 --> 00:12:06,480
to create a new file.
138

139
00:12:06,570 --> 00:12:13,650
So this is going to be, again, chapter1.txt. And in my new file, I'm just going to say, 
139

140
00:12:13,650 --> 00:12:14,930
"This is a story."
140

141
00:12:15,630 --> 00:12:20,950
I think I'm pretty much given up on writing stories now and we're going to make a commit.
141

142
00:12:21,030 --> 00:12:24,030
So this is going to be
142

143
00:12:24,060 --> 00:12:28,360
"Create chapter 1" and we're going to commit directly to the master branch.
143

144
00:12:28,440 --> 00:12:30,260
So commit new file.
144

145
00:12:30,870 --> 00:12:37,020
Okay, so now that we've got chapter1 and we are on the branch master, at this point, I'm going to create
145

146
00:12:37,080 --> 00:12:41,580
a new branch, and let's call this new branch experimental.
146

147
00:12:41,880 --> 00:12:48,660
Okay, so now we have experimental and it takes you straight to the experimental branch, copying over everything
147

148
00:12:48,660 --> 00:12:52,310
that you had at that point inside the master branch.
148

149
00:12:52,620 --> 00:12:57,530
And now inside this experimental branch, I'm going to make an edit to my chapter1.
149

150
00:12:57,560 --> 00:13:03,690
So I'm going to click on the file, not over here, but over here. And I'm going to click on the pencil button
150

151
00:13:03,750 --> 00:13:05,500
to edit this file.
151

152
00:13:05,520 --> 00:13:14,740
"This is a story." "This--" Nothing like a bit of confidence in your story writing to make it better,
152

153
00:13:14,740 --> 00:13:15,580
right?
153

154
00:13:15,850 --> 00:13:25,550
Okay, so now let's commit these changes and let's say, "Update chapter 1 to be more upbeat."
154

155
00:13:26,090 --> 00:13:28,900
Okay. Now, we're going to commit and we're committing.
155

156
00:13:29,020 --> 00:13:33,640
So always check to see where you're committing to because sometimes when you have a lot of branches
156

157
00:13:33,760 --> 00:13:37,070
and you're switching branches, then it can get quite confusing.
157

158
00:13:37,270 --> 00:13:43,390
So we're committing to the experimental branch and we're going to go ahead and commit those changes.
158

159
00:13:43,390 --> 00:13:50,500
Now, let's switch back to our master branch. And you can see immediately, this is reflected, so that new
159

160
00:13:50,500 --> 00:13:53,610
line that we had previously is now gone.
160

161
00:13:53,710 --> 00:13:56,230
So let's go back to our main repository file.
161

162
00:13:56,230 --> 00:14:00,520
You can see that you've already got this little notification here which is telling you that you've got
162

163
00:14:00,610 --> 00:14:08,660
a experimental branch and you can compare between your master and experimental and make a pull request.
163

164
00:14:08,710 --> 00:14:11,800
So we're going to talk about pull requests in more detail later on.
164

165
00:14:11,950 --> 00:14:17,260
But, basically, this is telling you, "Hey, you've got changes that are not reflected in your master branch.
165

166
00:14:17,290 --> 00:14:21,010
Do you want to pull those changes over to your master branch?"
166

167
00:14:21,040 --> 00:14:22,870
So right now, that's not we're going to do
167

168
00:14:22,960 --> 00:14:25,180
but instead, on our master brunch,
168

169
00:14:25,180 --> 00:14:27,760
So check to make sure that master is checked.
169

170
00:14:27,910 --> 00:14:34,660
We're going to create a new file and we're going to call it chapter2.txt and we're going to
170

171
00:14:34,660 --> 00:14:42,490
say, "This is an even better chapter." 
171

172
00:14:42,500 --> 00:14:53,210
Okay. So let's say, "Create chapter 2"  and we're committing to the master branch. So commit that file, and then
172

173
00:14:53,210 --> 00:14:59,450
we're going to check out what the structure of our repository looks like now, inside Graphs and inside
173

174
00:14:59,510 --> 00:15:00,460
Network.
174

175
00:15:00,740 --> 00:15:09,830
So you can see that we've got a initial commit, and then we have our created chapter1. And then after chapter1,
175

176
00:15:09,830 --> 00:15:18,080
we decide to make a brunch that was experimental, and then we continued where we left off in the master
176

177
00:15:18,080 --> 00:15:19,130
branch.
177

178
00:15:19,130 --> 00:15:26,720
So, now what I want to do is I want to go into that master branch and I want to merge those changes in
178

179
00:15:26,720 --> 00:15:28,550
the experimental branch over.
179

180
00:15:28,580 --> 00:15:33,930
So I want to pull those changes from the experimental branch to the master branch.
180

181
00:15:34,220 --> 00:15:40,130
So in order to pull, you have to be in the place that you're going to do the pulling which is the master
181

182
00:15:40,130 --> 00:15:41,060
branch.
182

183
00:15:41,060 --> 00:15:47,380
And while we're here, I'm going to say "New pull request," and this takes me to the Compare page.
183

184
00:15:47,450 --> 00:15:53,960
So here I can say that my base is my master branch and I want to compare it with my experimental branch.
184

185
00:15:54,650 --> 00:16:02,720
So we can see that the difference is only this line, so plus this line. When it's red, it's an erasure.
185

186
00:16:02,720 --> 00:16:03,920
When it's a plus,
186

187
00:16:03,920 --> 00:16:04,700
it's an addition.
187

188
00:16:04,700 --> 00:16:11,790
So whereas inside my master branch, this is all I've got inside chapter1. In my experimental branch,
188

189
00:16:11,810 --> 00:16:13,240
I've got all of this.
189

190
00:16:13,370 --> 00:16:17,530
So I have to say that I prefer this version in the experimental branch.
190

191
00:16:17,570 --> 00:16:21,190
So I'm very happy with what I did in the experiment.
191

192
00:16:21,320 --> 00:16:27,670
So I'm going to create a pull request that is going to merge these two branches.
192

193
00:16:27,680 --> 00:16:32,020
So, now once I've done that, I'm going to merge those two branches.
193

194
00:16:32,120 --> 00:16:42,500
So I'm going to say, "Merge experimental branch to master." And go ahead and confirm the merge.
194

195
00:16:42,500 --> 00:16:49,240
Now, you can see it's almost more straightforward doing it on the Terminal, but you get used to the interface,
195

196
00:16:49,350 --> 00:16:51,380
and this is not so complex either.
196

197
00:16:51,380 --> 00:16:54,440
So, now if we have a look at our Network graph,
197

198
00:16:57,590 --> 00:17:04,040
you can see that that experimental branch was pulled back to the master branch and merged.
198

199
00:17:04,130 --> 00:17:07,790
So that was us creating a branch and merging it.
199

200
00:17:07,790 --> 00:17:13,950
Now, why don't you go ahead and have a go and create your own repository, both locally and as well as
200

201
00:17:13,950 --> 00:17:14,920
on GitHub
201

202
00:17:15,050 --> 00:17:23,330
and, you know, write a story or write a poem, anything you like, and check out the amazingness that is
202

203
00:17:23,420 --> 00:17:25,070
branching using Git.
