WEBVTT

0
00:00.140 --> 00:05.120
Now, in the last lesson, we spoke about some of the more basic variants of version control.

1
00:05.150 --> 00:09.110
In this lesson, I want to talk about Branches and Branching.

2
00:09.140 --> 00:11.580
Now let's start off with a simple example,

3
00:11.600 --> 00:14.990
say if we had Version 1 and 2,

4
00:15.020 --> 00:18.800
so two commits that were made to our local repository,

5
00:18.800 --> 00:25.610
and at this point, we realize that we want to maybe try out something different, maybe build a new feature,

6
00:25.610 --> 00:28.730
or just to mess around with a new idea or concept.

7
00:28.760 --> 00:35.690
What we can do is instead of continuing to commit to the main branch that you see here, we can also

8
00:35.690 --> 00:39.470
create a side branch. After the second commit,

9
00:39.500 --> 00:46.880
we create a new branch and we start committing to this new branch or this experimental branch.

10
00:46.910 --> 00:50.150
We add some features and we write some code.

11
00:50.330 --> 00:56.810
Simultaneously, we can continue working on the main branch, putting out all those essential updates

12
00:56.810 --> 01:01.860
or bits of code that are maintaining our main project,

13
01:01.860 --> 01:08.520
but at the same time we can continue to update and work on this experimental branch trying things out

14
01:08.520 --> 01:11.840
and committing our experiments to this experimental branch.

15
01:11.850 --> 01:18.610
So now we have two branches that are parallel to each other and they can be developed simultaneously.

16
01:18.630 --> 01:24.900
If at some point in the future that we decide that that experiment was really fruitful, and the feature

17
01:24.900 --> 01:30.930
that we built in it was really, really great and we'd like to merge it back to the main branch or

18
01:30.930 --> 01:37.350
to the main project, then that can be done really easily as well by simply placing a merge request

19
01:37.350 --> 01:44.370
in. And we can bring all of those changes that we experimented with, that we messed around with, back to

20
01:44.370 --> 01:49.200
the main project and check to see if there's any conflict with the main branch code

21
01:49.200 --> 01:55.740
and if not, or if after a little bit of editing, then we can bring all of those changes into the main

22
01:55.740 --> 02:03.870
working branch. And then we can continue working from here onto to the next commit or we can make more

23
02:03.870 --> 02:04.620
branches.

24
02:04.620 --> 02:10.650
And very often what you see in practice is that there'll be multiple branches being worked on at the

25
02:10.650 --> 02:13.350
same time for any given large project.

26
02:13.350 --> 02:19.950
And the reason is because sometimes you're developing new features, sometimes you are fixing bugs and

27
02:19.950 --> 02:25.350
all of these things may break your main project, so you don't want to do it on the main branch.

28
02:25.350 --> 02:28.230
You only want to put it onto the main branch

29
02:28.230 --> 02:35.130
once you know that everything is working fine, and then you can bring your code back to the working copy

30
02:35.130 --> 02:37.410
ready for shipment and deployment.

31
02:37.530 --> 02:41.370
Let's take a look at how this would work in reality.

32
02:41.400 --> 02:49.890
Now let's say we navigate back to our Story directory, and inside here we've still got our previous

33
02:49.890 --> 02:53.430
three chapters and they are under version control.

34
02:53.430 --> 02:59.880
So if we just have a look at "git log", you can see that this is currently the most recent commit and

35
02:59.880 --> 03:02.580
it's also mirrored in our remote.

36
03:03.470 --> 03:06.260
Let's try and do this locally first.

37
03:06.410 --> 03:14.120
If I decided that I want to create a new branch, you can simply just write "git branch" and specify the

38
03:14.120 --> 03:16.050
name of your new branch.

39
03:16.070 --> 03:25.580
I'm going to add a space-related plot to my story, so let's call our branch, "alien-plot."

40
03:27.000 --> 03:27.840
Hit Enter.

41
03:27.840 --> 03:34.710
And now you can check out what branches you have by just writing "git branch" without the name and you

42
03:34.710 --> 03:40.440
can see that you've got one branch called, alien-plot and another one called main, and the asterisks (*)

43
03:40.440 --> 03:43.470
shows you which branch you are currently on.

44
03:43.470 --> 03:45.330
So we're currently on the main branch.

45
03:45.360 --> 03:50.940
We can switch to the alien-plot by simply writing "git checkout

46
03:51.780 --> 03:53.700
alien-plot".

47
03:56.580 --> 03:57.110
Okay.

48
03:57.120 --> 04:01.200
As it says, we have now switched to the branch called, alien-plot.

49
04:01.350 --> 04:07.290
Now, it is inside this branch that I'm going to make some changes to my chapters.

50
04:07.470 --> 04:14.730
Now I'm going to make some completely nonsensical modification to my files.

51
04:14.730 --> 04:16.680
So let's change chapter 1.

52
04:16.680 --> 04:19.860
Let's change chapter2.

53
04:26.520 --> 04:27.690
Okay.

54
04:27.870 --> 04:28.540
There we go.

55
04:28.560 --> 04:34.770
Okay, so we've made some modifications to chapter1 and chapter2, and we've decided to change some

56
04:34.770 --> 04:41.070
of the pre-existing written text to integrate a space-themed or alien-themed plot.

57
04:42.030 --> 04:44.220
Let's go ahead and commit that.

58
04:44.220 --> 04:55.500
So remember, we have to do "git add ." and then "git commit -m..." and our message is going to be "modify

59
04:56.010 --> 04:57.390
chapter 1

60
04:58.830 --> 05:04.950
and 2 to have alien theme".

61
05:06.490 --> 05:10.870
Okay, now hit Enter and we've made our commits.

62
05:11.020 --> 05:18.790
Now if we do a "git log", we can see that we have two commits made on the main branch and we have one

63
05:18.790 --> 05:21.970
commit made on the alien plot-branch.

64
05:22.120 --> 05:25.890
So now let's say that we go back onto our main branch.

65
05:25.900 --> 05:32.170
Whenever you're confused where you are, you can always do a "git branch" to check and see where the asterisks

66
05:32.170 --> 05:32.410
is.

67
05:32.410 --> 05:35.200
And the asterisks is obviously where you are.

68
05:35.230 --> 05:43.810
So let's do a "git checkout main" to go back to the main branch. So you can see that our main branch is

69
05:43.810 --> 05:44.740
unchanged

70
05:44.740 --> 05:48.790
with respect to the space or alien plot that we did just now.

71
05:48.790 --> 05:51.040
Nothing has changed over here.

72
05:51.040 --> 05:54.700
So while on the main branch, I'm going to create a new file,

73
05:54.700 --> 05:56.440
I'm going to call it chapter4.txt.

74
05:58.040 --> 06:03.690
And inside chapter four,

75
06:03.710 --> 06:05.300
let's add something.

76
06:09.640 --> 06:10.090
Okay.

77
06:10.090 --> 06:11.920
And hit, Enter and Save.

78
06:11.950 --> 06:13.600
By the way, I have no idea what I'm typing.

79
06:13.600 --> 06:18.600
I'm now making up and destroying, probably in the process a masterpiece, but it's okay.

80
06:18.610 --> 06:25.060
So we now have a chapter4 on our main branch and I'm going to go ahead and do a "git add ." and "git

81
06:25.060 --> 06:26.110
commit".

82
06:26.830 --> 06:32.230
Now there's ways of combining add and commit together in the same command, but I think if you're new

83
06:32.230 --> 06:36.070
to git, it's always good to really know in your head exactly what's going on.

84
06:36.070 --> 06:39.370
So I recommend actually separating those two bits out.

85
06:39.400 --> 06:42.340
Let's go ahead and give it a commit message.

86
06:42.520 --> 06:45.130
"add chapter 4."

87
06:45.970 --> 06:47.760
Okay, so "git log".

88
06:47.770 --> 06:48.400
Let's see,

89
06:48.400 --> 06:49.240
what have we got?

90
06:49.270 --> 06:53.470
We are currently only looking at the main branch and we have three commits.

91
06:53.500 --> 06:57.250
This is the position of our remote.

92
06:57.250 --> 07:02.650
So on our GitHub repository, this was the latest commit that it could see.

93
07:02.650 --> 07:08.410
But on our local git repository, this is in fact the latest commit, the one that we just made where

94
07:08.410 --> 07:09.920
we added chapter4.

95
07:09.920 --> 07:16.640
And you can see that if I switch between the branches, say, if I go over to alien-plot branch and hit

96
07:16.640 --> 07:22.120
Enter, you can see that our local files actually changed as I switched branches.

97
07:22.130 --> 07:29.030
Let's just say that I'm quite happy with the changes that I've made in terms of my alien-plot addition

98
07:29.030 --> 07:33.920
and I would like to merge these changes back into my main branch.

99
07:33.920 --> 07:37.550
So I've done a little bit of experimentation on a separate branch,

100
07:37.550 --> 07:42.050
I've, you know, messed with a few things and I think it was a great experiment.

101
07:42.050 --> 07:46.130
So I'm going to put it back into our main branch.

102
07:46.220 --> 07:50.870
In order to do that, what you have to do is go back to the main branch.

103
07:50.870 --> 07:59.000
So "git checkout main" and while we're on the main branch we're going to merge the changes inside the

104
07:59.000 --> 08:00.260
alien-plot branch.

105
08:00.260 --> 08:09.890
So we use the command "git merge" and we're going to specify the branch name which was, "alien-plot" hit

106
08:09.890 --> 08:10.400
Enter.

107
08:10.400 --> 08:16.550
And this opens up Vim, which is a text editor, and this allows you to add a merge message if you wish.

108
08:16.550 --> 08:20.810
And alternatively, as we're going to do here, we're just going to leave it empty and you're going

109
08:20.810 --> 08:24.500
to write, ":q!" to save and quit.

110
08:25.380 --> 08:31.440
And you can see now I've actually absorbed those changes from the alien plot.

111
08:31.530 --> 08:37.800
And you can see that because if I do "git branch", I am on the main branch.

112
08:37.800 --> 08:39.600
The alien plot branch still exists

113
08:39.600 --> 08:45.480
by the way. If you have a look at "git log", you can see that I've got some of the previous commits,

114
08:45.480 --> 08:51.180
but I've also got this merge branch alien-plot which was my most recent commit.

115
08:51.210 --> 09:00.960
Now, at this point let's do a "git push" to our "origin main" and remember to add the "-u" flag.

116
09:02.090 --> 09:03.110
That's completed.

117
09:03.110 --> 09:05.110
And let's check it out online.

118
09:05.120 --> 09:10.760
If we go over to our story repository, you can see that there's now five commits.

119
09:11.000 --> 09:13.010
We modified Chapter 1 and 2.

120
09:13.040 --> 09:17.150
We added chapter 4 and we merged the alien-plot branch.

121
09:17.150 --> 09:24.290
So if you go into insights and go to Network now, if I zoom in on this Network graph, then you can

122
09:24.290 --> 09:27.590
see this is the process that we've gone through.

123
09:27.770 --> 09:32.030
This is the main branch where we did chapter 1 and chapter 2

124
09:32.030 --> 09:39.200
and then at this point, after I created chapter 2 and 3, I created a new branch.

125
09:39.200 --> 09:45.380
And while inside this branch I made a commit that modified chapter 1 and 2 to have an alien theme.

126
09:45.380 --> 09:51.350
And then on the main branch, I continued developing the main branch, adding a chapter 4,

127
09:51.350 --> 10:00.800
but subsequently, I realized that I did a great job adding alien themes to our storyline and I decided

128
10:00.800 --> 10:04.410
to merge it back into the main branch.

129
10:04.410 --> 10:08.850
This is basically a graphical representation of what's been going on.

130
10:09.000 --> 10:12.750
So that was us creating a branch and merging it.

131
10:12.780 --> 10:19.590
Now, why don't you go ahead and have a go and create your own repository both locally as well as on GitHub?

132
10:19.590 --> 10:27.420
And, you know, write a story or write a poem, anything you like, and check out the amazingness that

133
10:27.420 --> 10:29.640
is Branching using git.